Adding new node or value at the end of LinkedList in C#

The LinkedList<T> class in C# provides the AddLast() method to add new nodes or values at the end of the linked list. This method maintains the sequential order and automatically updates the internal node structure.

Syntax

Following is the syntax for adding elements at the end of a LinkedList −

LinkedList<T> list = new LinkedList<T>();
list.AddLast(value);

The AddLast() method has two overloads −

public LinkedListNode<T> AddLast(T value)
public void AddLast(LinkedListNode<T> node)

Parameters

  • value − The value to add at the end of the LinkedList.

  • node − The LinkedListNode<T> to add at the end of the LinkedList.

Return Value

The first overload returns the LinkedListNode<T> containing the value. The second overload returns void.

AddLast() Operation A B C AddLast("D") D New node added at the end

Using AddLast() with String Values

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<String> list = new LinkedList<String>();
      list.AddLast("A");
      list.AddLast("B");
      list.AddLast("C");
      list.AddLast("D");
      list.AddLast("E");
      list.AddLast("F");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList...");
      foreach (string res in list) {
         Console.WriteLine(res);
      }
      list.AddLast("G");
      list.AddLast("H");
      list.AddLast("I");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList...");
      foreach (string res in list) {
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Count of nodes = 6
Elements in LinkedList...
A
B
C
D
E
F
Count of nodes = 9
Elements in LinkedList...
A
B
C
D
E
F
G
H
I

Using AddLast() with Integer Values

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<int> list = new LinkedList<int>();
      list.AddLast(100);
      list.AddLast(200);
      list.AddLast(300);
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList...");
      foreach (int res in list) {
         Console.WriteLine(res);
      }
      list.AddLast(400);
      list.AddLast(500);
      list.AddLast(600);
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("Elements in LinkedList...");
      foreach (int res in list) {
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Count of nodes = 3
Elements in LinkedList...
100
200
300
Count of nodes = 6
Elements in LinkedList...
100
200
300
400
500
600

Using AddLast() with LinkedListNode

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<string> list = new LinkedList<string>();
      
      LinkedListNode<string> node1 = new LinkedListNode<string>("First");
      LinkedListNode<string> node2 = new LinkedListNode<string>("Second");
      LinkedListNode<string> node3 = new LinkedListNode<string>("Third");
      
      list.AddLast(node1);
      list.AddLast(node2);
      list.AddLast(node3);
      
      Console.WriteLine("LinkedList contents:");
      foreach (string item in list) {
         Console.WriteLine(item);
      }
      
      Console.WriteLine("\nLast node value: " + list.Last.Value);
   }
}

The output of the above code is −

LinkedList contents:
First
Second
Third

Last node value: Third

Conclusion

The AddLast() method in C# LinkedList efficiently adds new nodes at the end of the list. It supports both direct values and LinkedListNode objects, making it versatile for different programming scenarios while maintaining O(1) time complexity.

Updated on: 2026-03-17T07:04:36+05:30

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements