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.
Constructors
Constructor |
Description |
LinkedList() |
Initializes a new instance of the LinkedList class that is empty. |
LinkedList(IEnumerable) |
Initializes a new instance of the LinkedList class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied. |
LinkedList(SerializationInfo, StreamingContext) |
Initializes a new instance of the LinkedList class that is serializable with the specified SerializationInfo and StreamingContext. |
Example:
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public static void Main()
{
LinkedList<String> myList = new LinkedList<String>();
myList.AddLast( "Geeks" );
myList.AddLast( "for" );
myList.AddLast( "Data Structures" );
myList.AddLast( "Noida" );
if (myList.Count > 0)
Console.WriteLine( "LinkedList is not empty" );
else
Console.WriteLine( "LinkedList is empty" );
}
}
|
Output:
LinkedList is not empty
Properties
Property |
Description |
Count |
Gets the number of nodes actually contained in the LinkedList. |
First |
Gets the first node of the LinkedList. |
Last |
Gets the last node of the LinkedList. |
Example:
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public static void Main()
{
LinkedList<String> myList = new LinkedList<String>();
myList.AddLast( "GeeksforGeeks" );
myList.AddLast( "GFG" );
myList.AddLast( "Data Structures" );
myList.AddLast( "Noida" );
if (myList.Count > 0)
Console.WriteLine(myList.First.Value);
else
Console.WriteLine( "LinkedList is empty" );
if (myList.Count > 0)
Console.WriteLine(myList.Last.Value);
else
Console.WriteLine( "LinkedList is empty" );
}
}
|
Output:
GeeksforGeeks
Noida
Methods
Method |
Description |
AddAfter |
Adds a new node or value after an existing node in the LinkedList. |
AddBefore |
Adds a new node or value before an existing node in the LinkedList. |
AddFirst |
Adds a new node or value at the start of the LinkedList. |
AddLast |
Adds a new node or value at the end of the LinkedList. |
Clear() |
Removes all nodes from the LinkedList. |
Contains(T) |
Determines whether a value is in the LinkedList. |
CopyTo(T[], Int32) |
Copies the entire LinkedList to a compatible one-dimensional Array, starting at the specified index of the target array. |
Equals(Object) |
Determines 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 an enumerator that iterates through the LinkedList. |
GetHashCode() |
Serves as the default hash function. |
GetObjectData(SerializationInfo, StreamingContext) |
Implements the ISerializable interface and returns the data needed to serialize the LinkedList instance. |
GetType() |
Gets the Type of the current instance. |
MemberwiseClone() |
Creates a shallow copy of the current Object. |
OnDeserialization(Object) |
Implements the ISerializable interface and raises the deserialization event when the deserialization is complete. |
Remove(LinkedListNode) |
Removes the specified node from the LinkedList. |
Remove(T) |
Removes the first occurrence of the specified value from the LinkedList. |
RemoveFirst() |
Removes the node at the start of the LinkedList. |
RemoveLast() |
Removes the node at the end of the LinkedList. |
ToString() |
Returns a string that represents the current object. |
Example:
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public static void Main()
{
LinkedList<String> myList = new LinkedList<String>();
myList.AddLast( "A" );
myList.AddLast( "B" );
myList.AddLast( "C" );
myList.AddLast( "D" );
myList.AddLast( "E" );
Console.WriteLine(myList.Contains( "B" ));
}
}
|
Output:
True
Example:
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public static void Main()
{
LinkedList< int > myList = new LinkedList< int >();
myList.AddLast(2);
myList.AddLast(4);
myList.AddLast(6);
myList.AddLast(8);
Console.WriteLine( "Total nodes in myList are : " + myList.Count);
foreach ( int i in myList)
{
Console.WriteLine(i);
}
myList.Remove(myList.First);
Console.WriteLine( "Total nodes in myList are : " + myList.Count);
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:
Last Updated :
20 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...