An example of how to use the C# LinkedList class

Linked lists <T> classes exist in the System.Collections.Generic namespace. This generic type allows for quick insertion and deletion of elements. It implements the classic linked list. Each object is assigned individually. In LinkedList, some operations don’t require duplicating the entire collection. But there are many common situations where LinkedList affects performance.

Features of the LinkedList class:

  • LinkedList <T> is a universally linked list. It supports enumerators.
  • Insertion and removal are O(1) operations.
  • You can delete nodes and reinsert them into the same list or another list, which will result in no other objects being assigned on the heap.
  • Since the list also maintains an internal count, getting the Count property is an O(1) operation.
  • The type of each node T> object in LinkedList is LinkedListNode <T>.
  • The LinkedList class does not support links, splits, loops, or other features that can leave the list in an inconsistent state.
  • If LinkedList is empty, the first and persistent attributes contain null.
  • LinkedList is doubly linked, so each node points to the next node and points to the previous node.

Table of Contents

Builder

LinkedList()Initialize a new instance of the empty LinkedList class.
LinkedList(IEnumerable)Initializes a new instance of the LinkedList class that contains the elements copied from the specified IEnumerable, and has enough capacity to accommodate the number of copied elements.
LinkedList(SerializationInfo, StreamingContext)Initializes a new instance of the LinkedList class, which can be serialized with the specified SerializationInfo and StreamingContext.

Example:

// C# code to create a LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
     //Driver code
     public static void Main()
     {
  
         //Creating a LinkedList of Strings
         LinkedList<String> myList = new LinkedList<String>();
  
         //Adding nodes in LinkedList
         myList.AddLast( "Geeks" );
         myList.AddLast( "for" );
         myList.AddLast( "Data Structures" );
         myList.AddLast( "Noida" );
  
         //To check if LinkedList is empty or not
         if (myList.Count> 0)
             Console.WriteLine( "LinkedList is not empty" );
         else
             Console.WriteLine( "LinkedList is empty" );
     }
}

The output is as follows:

LinkedList is not empty

Attribute

CountGet the number of nodes that are actually included in the LinkedList.
FirstGet the first node of LinkedList.
PersistentGet the last node of LinkedList.

Example:

// C# code to illustrate the
//LinkedList<T> class properties
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
     //Driver code
     public static void Main()
     {
  
         //Creating a LinkedList of Strings
         LinkedList<String> myList = new LinkedList<String>();
  
         //Adding nodes in LinkedList
         myList.AddLast( "lsbin" );
         myList.AddLast( "GFG" );
         myList.AddLast( "Data Structures" );
         myList.AddLast( "Noida" );
  
         //------- Count Property -------
          
         //To get the first node of the LinkedList
         if (myList.Count> 0)
             Console.WriteLine(myList.First.Value);
         else
             Console.WriteLine( "LinkedList is empty" );
              
         //------- Last Property -------
          
         //To get the last node of the LinkedList
         if (myList.Count> 0)
             Console.WriteLine(myList.Last.Value);
         else
             Console.WriteLine( "LinkedList is empty" );    
              
     }
}

The output is as follows:

lsbin
Noida

Method

AddAfterAdd a new node or value after an existing node in LinkedList.
AddBeforeAdd a new node or value before an existing node in LinkedList.
AddFirstAdd a new node or value at the beginning of your LinkedList.
AddLastAdd a new node or value at the end of the LinkedList.
Clear()Remove all nodes from LinkedList.
Contains(T)Determine if the value is in LinkedList.
CopyTo(T [], Int32)Start at the specified index of the target array and copy the entire LinkedList to a compatible one-dimensional array.
EqualsDetermines whether the specified object is equal to the current object.
Find(T)Finds the first node that contains the specified value.
FindLast(T)Finds the last node that contains the specified value.
GetEnumerator()Returns the number of enumerations that traversed the LinkedList.
GetHashCode()Used as the default hash function.
GetObjectData(SerializationInfo, StreamingContext)Implement the ISerializable interface and return the data required to serialize the LinkedList instance.
GetType()Obtain the type of the current instance.
MemberwiseClone()Creates a superficial copy of the current object.
OnDeserialization(Object)When deserialization is complete, the ISerializable interface is implemented and a deserialization event is raised.
Remove(LinkedListNode)Deletes the specified node from the LinkedList.
Remove(T)Deletes the specified value from the LinkedList for the first occurrence.
RemoveFirst()Delete the node at the beginning of LinkedList.
RemoveLast()Delete the node at the end of the LinkedList.
ToString()Returns a string representing the current object.

Example:

//C# code to check if a
//value is in LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
     //Driver code
     public static void Main()
     {
         //Creating a LinkedList of Strings
         LinkedList<String> myList = new LinkedList<String>();
  
         //Adding nodes in LinkedList
         myList.AddLast( "A" );
         myList.AddLast( "B" );
         myList.AddLast( "C" );
         myList.AddLast( "D" );
         myList.AddLast( "E" );
  
         //To check if a value is in LinkedList
         Console.WriteLine(myList.Contains( "B" ));
     }
}

The output is as follows:

True

Example:

//C# code to remove the specified
//node from the 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(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);
         }
  
         //Removing the first node from the LinkedList
         myList.Remove(myList.First);
  
         //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);
         }
     }
}

The output is as follows:

Total nodes in myList are : 4
2
4
6
8
Total nodes in myList are : 3
4
6
8