C# | Adding new node or value at the start of LinkedList<T>
LinkedList<T>.AddFirst Method is used to add a new node or value at the starting of the LinkedList<T>. There are 2 methods in the overload list of this method as follows:
- AddFirst(LinkedList<T>)
- AddFirst(T)
AddFirst(LinkedListNode<T>)
This method is used to add the specified new node at the starting of the LinkedList<T>.
Syntax:
public void AddFirst (System.Collections.Generic.LinkedListNode<T> node);
Here, node is the new LinkedListNode<T> to add at the start of the LinkedList<T>.
Exceptions:
- ArgumentNullException : If the node is null.
- InvalidOperationException : If the node belongs to another LinkedList<T>.
Example:
// C# code to add new node // at the start of LinkedList using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a LinkedList of Integers LinkedList< int > myList = new LinkedList< int >(); // Adding nodes in LinkedList myList.AddLast(2); myList.AddLast(4); myList.AddLast(6); myList.AddLast(6); myList.AddLast(6); myList.AddLast(8); // To get the count of nodes in LinkedList // before removing all the nodes Console.WriteLine( "Total nodes in myList are : " + myList.Count); // Displaying the nodes in LinkedList foreach ( int i in myList) { Console.WriteLine(i); } // Adding new node at the start of LinkedList // This will give error as node is null myList.AddFirst(5); // To get the count of nodes in LinkedList // after removing all the nodes Console.WriteLine( "Total nodes in myList are : " + myList.Count); // Displaying the nodes in LinkedList foreach ( int i in myList) { Console.WriteLine(i); } } } |
Output:
Total nodes in myList are : 6
2
4
6
6
6
8Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: node
Note:
- LinkedList<T> accepts null as a valid Value for reference types and allows duplicate values.
- If the LinkedList<T> is empty, the new node becomes the First and the Last.
- This method is an O(1) operation.
AddFirst(T) Method
This method is used to add a new node containing the specified value at the start of the LinkedList<T>.
Syntax:
public System.Collections.Generic.LinkedListNode AddFirst (T value);
Here, value is the value to add at the start of the LinkedList<T>.
Return Value: The new LinkedListNode<T> containing value.
Example:
// C# code to add new node containing // the specified value at the start // of LinkedList using System; using System.Collections; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a LinkedList of Integers LinkedList< int > myList = new LinkedList< int >(); // Adding nodes in LinkedList myList.AddLast(2); myList.AddLast(4); myList.AddLast(6); myList.AddLast(6); myList.AddLast(6); myList.AddLast(8); // To get the count of nodes in LinkedList // before removing all the nodes Console.WriteLine( "Total nodes in myList are : " + myList.Count); // Displaying the nodes in LinkedList foreach ( int i in myList) { Console.WriteLine(i); } // Adding new node containing the // specified value at the start of LinkedList myList.AddFirst(20); // To get the count of nodes in LinkedList // after removing all the nodes Console.WriteLine( "Total nodes in myList are : " + myList.Count); // Displaying the nodes in LinkedList foreach ( int i in myList) { Console.WriteLine(i); } } } |
Output:
Total nodes in myList are : 6 2 4 6 6 6 8 Total nodes in myList are : 7 20 2 4 6 6 6 8
Note:
- LinkedList<T> accepts null as a valid Value for reference types and allows duplicate values.
- If the LinkedList<T> is empty, the new node becomes the First and the Last.
- This method is an O(1) operation.
Reference:
Recommended Posts:
- C# | Adding new node or value at the end of LinkedList<T>
- C# | Removing the node at the start of the LinkedList<T>
- How to Create a Range From a Specified Start in C#?
- How to check whether the index is from start or end in C#?
- C# | Adding the specified key and value into HybridDictionary
- Finding the Start Index of the Specified Range in C#
- Finding all the Elements of a Range from Start to End in C#
- C# | Adding an element into the Hashtable
- C# | Adding elements to the end of the ArrayList
- C# | Adding an element to the List
- C# | Adding the elements to the end of the ArrayList
- C# | Adding the elements of the specified collection to the end of the List
- Creating an Index From the Specified Index at the Start of a Collection in C#
- C# | Get the first node of the LinkedList<T>
- C# | Get the last node of the LinkedList<T>
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.