C# | LinkedList Class

20 Dec, 2018
LinkedList<T> Class is present in System.Collections.Generic namespace. This generic type allows fast inserting and removing of elements. It implements a classic linked list. Each object is separately allocated. In the LinkedList, certain operations do not require the whole collection to be copied. But in many common cases LinkedList hinders performance.
Characteristics of LinkedList Class:
LinkedList<T> is a general-purpose linked list. It supports enumerators.
Insertion and removal are O(1) operations.
You can remove nodes and reinsert them, either in the same list or in another list, which results in no additional objects allocated on the heap.
Because the list also maintains an internal count, getting the Count property is an O(1) operation.
Each node in a LinkedList<T> object is of the type LinkedListNode<T>.
The LinkedList class does not support chaining, splitting, cycles, or other features that can leave the list in an inconsistent state.
If the LinkedList is empty, the First and Last properties contain null.
The LinkedList is doubly linked, therefore, each node points forward to the Next node and backward to the Previous node.
Example:
// C# code to create a LinkedListusing 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");    }}
Output:
LinkedList is not empty

Example:
// C# code to illustrate the// LinkedList<T> class propertiesusing 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("GeeksforGeeks");        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");                      }}
Output:
GeeksforGeeks
Noida

Example:
// C# code to check if a// value is in LinkedListusing 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"));    }}
Output:
True

Example:
// C# code to remove the specified// node from the LinkedListusing 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);        }    }}
Output:
Total nodes in myList are : 4
2
4
6
8
Total nodes in myList are : 3
4
6
8

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlist-1?view=netframework-4.7.2
CSharp-Generic-Namespace
CSharp-LinkedList
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to .NET Framework
C# | Delegates
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
C# | Data Types
C# | Constructors
C# | Class and Object
Extension Method in C#
C# | Encapsulation
C# | Replace() Method