VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-adding-new-node-or-value-at-the-start-of-linkedlistt/

⇱ C# | Adding new node or value at the start of LinkedList<T> - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# | Adding new node or value at the start of LinkedList<T>

Last Updated : 11 Jul, 2025
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:
  1. AddFirst(LinkedList<T>)
  2. 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: Output:
Total nodes in myList are : 6 2 4 6 6 6 8 Unhandled 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:
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:
Comment

Explore