LinkedList<T> in C# - GeeksforGeeks

LinkedList in C#

Last Updated : 26 Oct, 2026

The LinkedList<T> class in C# is a generic collection that represents a doubly linked list. Each element is stored in a node that contains the element and references to the previous and next nodes. It is defined in the System.Collections.Generic namespace and is useful when elements need to be frequently inserted or removed from the collection.

  • Stores elements as nodes connected through references to the previous and next nodes.
  • Allows elements to be added or removed from the beginning, end, or at a specific node.
  • Provides efficient insertion and removal when the position is already known through a LinkedListNode<T>.
CSharp-LinkedList
LinkedList Representation in C#

Example: The following example demonstrates how to create a LinkedList<T>, add elements, and display them.

C#
using System;
using System.Collections.Generic;

public class Geeks
{
    public static void Main()
    {
        // Create a new LinkedList
        LinkedList<int> list = new LinkedList<int>();

        // Add elements to the LinkedList
        list.AddLast(10);
        list.AddLast(20);
        list.AddLast(30);
        list.AddLast(40);

        // Display elements
        foreach (int item in list)
        {
            Console.WriteLine(item);
        }
    }
}

Output
10
20
30
40

Explanation

  • Elements are added to the LinkedList<T> using the AddLast() method.
  • The foreach loop is used to iterate through the elements of the linked list.
  • LinkedList<T> maintains the order of elements in which they are linked.

Declaration

The LinkedList<T> class can be declared by creating an instance of the LinkedList<T> class.

Syntax

LinkedList<T> listName = new LinkedList<T>();

Here, T specifies the type of elements that the linked list can store.

Hierarchy of LinkedList<T>

The LinkedList<T> class belongs to the System.Collections.Generic namespace and implements the ICollection<T>, IEnumerable<T>, IEnumerable, and IReadOnlyCollection<T> interfaces.

1
Hierarchy of LinkedList

Constructors of LinkedList<T>

The LinkedList<T> class provides constructors for creating an empty linked list or initializing it with elements from another collection.

1. LinkedList<T>()

Creates an empty LinkedList<T>.

LinkedList<int> list = new LinkedList<int>();

2. LinkedList<T>(IEnumerable<T>)

Creates a LinkedList<T> containing elements copied from the specified collection.

int[] numbers = { 10, 20, 30 };

LinkedList<int> list = new LinkedList<int>(numbers);

Example: The following example demonstrates different ways to initialize a LinkedList<T>.

C#
using System;
using System.Collections.Generic;

public class Geeks
{
    public static void Main()
    {
        // Create an empty LinkedList
        LinkedList<int> list1 = new LinkedList<int>();

        // Create a LinkedList from an array
        int[] numbers = { 10, 20, 30 };
        LinkedList<int> list2 = new LinkedList<int>(numbers);

        Console.WriteLine("LinkedList 1 Count: " + list1.Count);
        Console.WriteLine("LinkedList 2 Count: " + list2.Count);
    }
}

Output
LinkedList 1 Count: 0
LinkedList 2 Count: 3

Properties of LinkedList<T>

The LinkedList<T> class provides properties to access the first and last nodes and determine the number of elements in the collection.

PropertyDescription
CountGets the number of nodes actually contained in the LinkedList<T>.
FirstGets the first node of the LinkedList<T>.
LastGets the last node of the LinkedList<T>.

Example: The following example demonstrates the Count, First, and Last properties of the LinkedList<T> class.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    static void Main()
    {
        // Create a LinkedList
        LinkedList<string> list = new LinkedList<string>();

        // Add elements
        list.AddLast("Apple");
        list.AddLast("Banana");
        list.AddLast("Mango");

        // Display properties
        Console.WriteLine("Count: " + list.Count);
        Console.WriteLine("First: " + list.First.Value);
        Console.WriteLine("Last: " + list.Last.Value);
    }
}

Output
Count: 3
First: Apple
Last: Mango

Performing Different Operations on LinkedList<T>

The LinkedList<T> class supports operations for adding, accessing, searching, and removing elements.

1. Adding Elements

The AddFirst() and AddLast() methods are used to add elements at the beginning and end of the LinkedList<T>. The AddBefore() and AddAfter() methods can be used to insert elements relative to an existing node.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Create a LinkedList
        LinkedList<string> list = new LinkedList<string>();

        // Add elements
        list.AddLast("For");
        list.AddLast("Geeks");
        list.AddFirst("Geeks");

        // Display the LinkedList
        foreach (string item in list)
        {
            Console.WriteLine(item);
        }
    }
}

Output
Geeks
For
Geeks

2. Accessing the Elements

The First and Last properties can be used to access the first and last nodes of the LinkedList<T>. The Value property of a LinkedListNode<T> is used to access the value stored in a node.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Create a LinkedList
        LinkedList<int> list = new LinkedList<int>();

        // Add elements
        list.AddLast(10);
        list.AddLast(20);
        list.AddLast(30);

        // Access the first and last elements
        Console.WriteLine("First element: " + list.First.Value);
        Console.WriteLine("Last element: " + list.Last.Value);
    }
}

Output
First element: 10
Last element: 30

3. Removing Elements

The LinkedList<T> class provides five methods to remove elements:

  • Clear(): Removes all nodes from the LinkedList<T>.
  • Remove(LinkedListNode<T>): Removes the specified node from the LinkedList<T>.
  • Remove(T): Removes the first occurrence of the specified value from the LinkedList<T>.
  • RemoveFirst(): Removes the first node from the LinkedList<T>.
  • RemoveLast(): Removes the last node from the LinkedList<T>.
C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Create a LinkedList
        LinkedList<int> list = new LinkedList<int>();

        // Add elements
        list.AddLast(10);
        list.AddLast(20);
        list.AddLast(30);
        list.AddLast(40);
        list.AddLast(50);

        // Remove the specified node
        LinkedListNode<int> node = list.Find(20);
        list.Remove(node);

        // Remove the first occurrence of the specified value
        list.Remove(30);

        // Remove the first node
        list.RemoveFirst();

        // Remove the last node
        list.RemoveLast();

        // Display the LinkedList
        foreach (int item in list)
        {
            Console.WriteLine(item);
        }

        // Remove all nodes
        list.Clear();

        Console.WriteLine("Elements after Clear(): " + list.Count);
    }
}

Output
40
Elements after Clear(): 0

4. Searching for Elements

The Contains() method checks whether a specified value exists in the LinkedList<T>. The Find() and FindLast() methods are used to locate the first and last nodes containing the specified value.

C#
using System;
using System.Collections.Generic;

class Geeks
{
    public static void Main()
    {
        // Create a LinkedList
        LinkedList<int> list = new LinkedList<int>();

        // Add elements
        list.AddLast(10);
        list.AddLast(20);
        list.AddLast(30);
        list.AddLast(20);
        list.AddLast(40);

        // Check whether an element exists
        Console.WriteLine("Contains 20: " + list.Contains(20));

        // Find the first node containing 20
        LinkedListNode<int> firstNode = list.Find(20);

        // Find the last node containing 20
        LinkedListNode<int> lastNode = list.FindLast(20);

        Console.WriteLine("First occurrence: " + firstNode.Value);
        Console.WriteLine("Last occurrence: " + lastNode.Value);
    }
}

Output
Contains 20: True
First occurrence: 20
Last occurrence: 20

Applications of LinkedList<T>

The LinkedList<T> class is useful when applications require frequent insertion and removal of elements, especially when the position of the node is already known.

  • Task Management: Maintains a sequence of tasks where elements can be efficiently added or removed.
  • Undo and Redo Operations: Maintains previous and next states, allowing movement in both directions.
  • Navigation Systems: Stores items that can be traversed forward and backward.
  • Deque Implementation: Can be used to implement a double-ended queue with insertion and removal at both ends.
  • Music Playlists: Manages songs where tracks can be added or removed from different positions.

Methods of LinkedList<T>

The important methods of the LinkedList<T> class are:

MethodDescription
AddAfter(LinkedListNode<T>, T)Adds a new node containing the specified value after the specified node in the LinkedList<T>.
AddAfter(LinkedListNode<T>, LinkedListNode<T>)Adds the specified node after the specified node in the LinkedList<T>.
AddBefore(LinkedListNode<T>, T)Adds a new node containing the specified value before the specified node in the LinkedList<T>.
AddBefore(LinkedListNode<T>, LinkedListNode<T>)Adds the specified node before the specified node in the LinkedList<T>.
AddFirst(T)Adds a new node containing the specified value at the beginning of the LinkedList<T>.
AddLast(T)Adds a new node containing the specified value at the end of the LinkedList<T>.
Clear()Removes all nodes from the LinkedList<T>.
Contains(T)Determines whether the specified value exists in the LinkedList<T>.
CopyTo(T[], Int32)Copies the elements of the LinkedList<T> to an existing one-dimensional array, starting at the specified array index.
Find(T)Finds the first node that contains the specified value.
FindLast(T)Finds the last node that contains the specified value.
GetEnumerator()Returns an enumerator that iterates through the LinkedList<T>.
Remove(LinkedListNode<T>)Removes the specified node from the LinkedList<T>.
Remove(T)Removes the first occurrence of the specified value from the LinkedList<T>.
RemoveFirst()Removes the first node from the LinkedList<T>.
RemoveLast()Removes the last node from the LinkedList<T>.
Comment

Explore