10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

LinkedList Collection in C#

Updated on Sep 3, 2025
 
45,998 Views

The LinkedList Collection represents a doubly linked list. It allows insertion, deletion and other operations from the linked list. This collection is found in System.Collections.Generic namespace.

Constructors in LinkedList Collection

The different constructors and their description is given as follows:

Constructors

Description

LinkedList<T>()

This constructor initializes a new instance of the LinkedList<T> class that is empty.

LinkedList<T>(IEnumerable<T>)

This constructor initializes a new instance of the LinkedList<T> class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied.

LinkedList<T>(SerializationInfo, StreamingContext)

This constructor initializes a new instance of the LinkedList<T> class that is serializable with the specified SerializationInfo and StreamingContext.

Table: Constructors in LinkedList Collection in C#
Source: MSDN

Properties in LinkedList Collection

The different properties and their description is given as follows:

Properties

Description

Count

This property gets the number of nodes actually contained in the LinkedList<T>.

First

This property gets the first node of the LinkedList<T>.

Last

This property gets the last node of the LinkedList<T>.

Table: Properties in LinkedList Collection in C#
Source: MSDN

Methods in LinkedList Collection

The different methods and their description is given as follows:

Methods

Description

AddAfter(LinkedListNode<T>, LinkedListNode<T>)

This method adds the specified new node after the specified existing node in the LinkedList<T>.

AddAfter(LinkedListNode<T>, T)

This method adds a new node containing the specified value after the specified existing node in the LinkedList<T>.

AddBefore(LinkedListNode<T>, LinkedListNode<T>)

This method adds the specified new node before the specified existing node in the LinkedList<T>.

AddBefore(LinkedListNode<T>, T)

This method adds a new node containing the specified value before the specified existing node in the LinkedList<T>.

AddFirst(LinkedListNode<T>)

This method adds the specified new node at the start of the LinkedList<T>.

AddFirst(T)

This method adds a new node containing the specified value at the start of the LinkedList<T>.

AddLast(LinkedListNode<T>)

This method adds the specified new node at the end of the LinkedList<T>.

AddLast(T)

This method adds a new node containing the specified value at the end of the LinkedList<T>.

Clear()

This method removes all the nodes from the LinkedList<T>.

Contains(T)

This method determines whether a value is in the LinkedList<T>.

Equals(Object)

This method determines whether the specified object is equal to the current object.

Find(T)

This method finds the first node that contains the specified value.

FindLast(T)

This method finds the last node that contains the specified value.

GetHashCode()

This method serves as the default hash function.

Remove(LinkedListNode<T>)

This method removes the specified node from the LinkedList<T>.

Remove(T)

This method removes the first occurrence of the specified value from the LinkedList<T>.

RemoveFirst()

This method removes the node at the start of the LinkedList<T>.

RemoveLast()

This method removes the node at the end of the LinkedList<T>.

ToString()

This method returns a string that represents the current object.

Table: Methods in LinkedList Collection in C#
Source: MSDN

Create a Linked List

Before beginning with the Linked List operations, let us first see how to create a simple Linked List:

using System;
using System.Collections.Generic;
class Example
{
   static void Main()
   {
       string [] empID = {"E0001","E0005","E0006"};
       Console.WriteLine("Displaying elements of a Linked List: ");
       LinkedList<string> myList = new LinkedList<string>(empID);                
        foreach (var res in myList)  
       {
           Console.WriteLine(res);  
       }
   }
}

Source Code: Program to create a Linked List in C#

The output of the above program is as follows:

Displaying elements of a Linked List:
E0001
E0005
E0006

Linked List Operations

Some of the Linked List operations are given as follows:

Adding elements in Linked List

An element is added to the start of the Linked List using the AddFirst() method. Similarly, the AddLast() method can be used to add the element to the end of the linked list. The program that demonstrates this is given as follows:

using System;
using System.Collections.Generic;
namespace LinkedListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        LinkedList <int> L = new LinkedList<int>();
        L.AddFirst(5);
        L.AddFirst(2);
        L.AddFirst(8);
        L.AddLast(4);
        L.AddLast(9);
        L.AddLast(1);
        Console.Write("Linked List elements are: ");
        foreach (int i in L)
        {
            Console.Write(i + " ");
        }
     }
  }
}

Source Code: Program to add elements in Linked List in C#

The output of the above program is as follows:

Linked List elements are: 8 2 5 4 9 1

Deleting elements from Linked List

An element can be deleted from the start of the Linked List using the RemoveFirst() method. Similarly, the RemoveLast() method can be used to delete the element from the end of the linked list. The program that demonstrates this is given as follows:

using System;
using System.Collections.Generic;
namespace LinkedListDemo  
{
  class Example
  {
     static void Main(string[] args)
     {
        LinkedList <int> L = new LinkedList<int>();
        L.AddFirst(5);
        L.AddFirst(2);
        L.AddFirst(8);
        L.AddLast(4);
        L.AddLast(9);
        L.AddLast(1);
        Console.Write("Original Linked List: ");
        foreach (int i in L)
        {
            Console.Write(i + " ");
        }
        L.RemoveFirst();
        L.RemoveLast();
        Console.WriteLine();
        Console.Write("Linked List after deletion: ");
        foreach (int i in L)
        {
            Console.Write(i + " ");
        }
     }
  }
}

Source Code: Program to delete elements from Linked List in C#

The output of the above program is as follows:

Original Linked List: 8 2 5 4 9 1
Linked List after deletion: 2 5 4 9

Finding an element in Linked List

The Contains method finds if an element is present in Linked List or not. If it is present, Contains() returns TRUE and otherwise it returns FALSE.

The program that demonstrates this is given as follows:

using System;
using System.Collections.Generic;
namespace LinkedListDemo  
{
  class Example
  {
     static void Main(string[] args)
     {
        LinkedList <int> list = new LinkedList<int>();
        list.AddLast(4);
        list.AddLast(9);
        list.AddLast(1);
        list.AddLast(3);
        list.AddLast(6);
        list.AddLast(2);
        Console.Write("Linked List: ");
        foreach (int i in list )
        {
            Console.Write(i + " ");
        }
        Console.WriteLine();
        Console.WriteLine("The value 3 is present in Linked List: " + list.Contains(3));
        Console.WriteLine("The value 5 is present in Linked List: " + list.Contains(5));
     }
  }
}

Source Code: Program to find an element in Linked List in C#

The output of the above program is as follows:

Linked List: 4 9 1 3 6 2
The value 3 is present in Linked List: True
The value 5 is present in Linked List: False

Add a node at the first position in a Linked List

Use the AddFirst() method to add a node at the first position in a Linked List. Let us see how:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Smartphones","Smartwatches"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       myList.AddFirst("Calculators");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
   }
}

Source Code: Program to add a node at the first position in a Linked List in C#

The output of the above program is as follows:

Smartphones
Smartwatches

New list...
Calculators
Smartphones
Smartwatches
using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Smartphones","Smartwatches"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       myList.AddFirst("Calculators");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       myList.AddLast("PDAs");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
   }
}

Add a node at the last position in a Linked List

Use the AddLast() method to add a node at the last position in a Linked List. Let us see how:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Smartphones","Smartwatches"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       myList.AddFirst("Calculators");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       myList.AddLast("PDAs");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
   }
}

Source Code: Program to add a node at the last position in a Linked List in C#

The above program gives the following output:

Smartphones
Smartwatches

New list...
Calculators
Smartphones
Smartwatches

New list...
Calculators
Smartphones
Smartwatches
PDAs

Add a node after the given node in a Linked List

If you want to add a node after the given node, use the AddAfter() method. Let us see an example:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Smartphones","Smartwatches"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       var n = myList.AddFirst("Calculators");
       myList.AddAfter(n, "PDAs");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
   }
}

Source Code: Program to add a node after the given node in a Linked List in C#

The output of the above program is as follows:

Smartphones
Smartwatches

New list...
Calculators
PDAs
Smartphones
Smartwatches

Add a node before the given node in a Linked List

If you want to add a node before the given node, use the AddBefore() method. Let us see an example:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Smartphones","Smartwatches"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       var n = myList.AddFirst("Calculators");
       myList.AddBefore(n, "PDAs");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
   }
}

Source Code: Program to add a node before the given node in a Linked List in C#

The above program gives the following output:

Smartphones
Smartwatches

New list...
PDAs
Calculators
Smartphones
Smartwatches

Check whether a node is in a Linked List or not

To check whether a node is in a Linked List or not, the best way is to use the Contains() method:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Smartphones","Smartwatches"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("Smartphones in the list?: "+myList.Contains("Smartphones"));
   }
}

Source Code: Program to check whether a node is in a Linked List or not in C#

The output of the given program:

Smartphones
Smartwatches
Smartphones in the list?: True

Remove the first occurrence of a node in a Linked List

Use the Remove() method to remove the first occurrence of a node. Let us see an example:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Smartphones","Smartwatches"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
       foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       var n = myList.AddFirst("Calculators");
       myList.AddBefore(n, "PDAs");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       myList.Remove("Smartwatches");
       Console.WriteLine("\nNew list after removing an element...");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
   }
}

Source Code: Program to remove the first occurrence of a node in C#

The above program gives the following output:

Smartphones
Smartwatches

New list...
PDAs
Calculators
Smartphones
Smartwatches

New list after removing an element...
PDAs
Calculators
Smartphones

Remove a node at the end of the Linked List

Use the RemoveLast() method to remove a node at the end of the Linked List.

The following is an example:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Smartphones","Smartwatches", "Laptop"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
       foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       var n = myList.AddFirst("Calculators");
       myList.AddBefore(n, "PDAs");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       myList.RemoveLast();
       Console.WriteLine("\nNew list after removing the last element...");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
   }
}

Source Code: Program to remove a node at the end of the Linked List in C#

The above program gives the following output:

Smartphones
Smartwatches
Laptop

New list...
PDAs
Calculators
Smartphones
Smartwatches
Laptop

New list after removing the last element...
PDAs
Calculators
Smartphones
Smartwatches

Remove a node at the beginning of a Linked List

Use the RemoveFirst() method to remove a node at the beginning of a Linked List. The following is an example demonstrating the same:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Monitors","Headphones", "Laptop"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
       foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       Console.WriteLine("\nNew list...");
       var n = myList.AddFirst("Printer");
       myList.AddBefore(n, "PDAs");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       myList.RemoveFirst();
       Console.WriteLine("\nNew list after removing the first node...");
        foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }   
   }
}

Source Code: Program to remove a node at the beginning of the Linked List in C#

The above program gives the following output:

Monitors
Headphones
Laptop

New list...
PDAs
Printer
Monitors
Headphones
Laptop

New list after removing the first node...
Printer
Monitors
Headphones
Laptop

Clear a Linked List

To clear a Linked List, use the Clear() method. Let us see an example:

using System;
using System.Collections.Generic;
class Demo
{
   static void Main()
   {
       string [] devices = {"Monitors","Headphones", "Laptop"};
       LinkedList<string> myList = new LinkedList<string>(devices);                
       foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
       myList.Clear();
       Console.WriteLine("\nLinked List cleared...");
       Console.WriteLine("\nNew list...");
       var n = myList.AddFirst("Printer");
       myList.AddBefore(n, "PDAs");
       foreach (var d in myList)  
       {
           Console.WriteLine(d);  
       }
   }
}

Source Code: Program to clear a Linked List in C#

The above program gives the following output:

Monitors
Headphones
Laptop

Linked List cleared...

New list...
PDAs
Printer
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses