A linked list helps you store items in order, where each item points to the next one. It is useful when you want to add or remove items easily without moving everything.
LinkedList usage in C Sharp (C#)
public class Node<T> { public T Value { get; set; } public Node<T>? Next { get; set; } public Node(T value) { Value = value; Next = null; } } public class LinkedList<T> { private Node<T>? head; public LinkedList() { head = null; } public void AddFirst(T value) { Node<T> newNode = new Node<T>(value); newNode.Next = head; head = newNode; } public void AddLast(T value) { Node<T> newNode = new Node<T>(value); if (head == null) { head = newNode; return; } Node<T> current = head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } public bool Remove(T value) { if (head == null) return false; if (head.Value!.Equals(value)) { head = head.Next; return true; } Node<T>? current = head; while (current.Next != null && !current.Next.Value!.Equals(value)) { current = current.Next; } if (current.Next == null) return false; current.Next = current.Next.Next; return true; } public void PrintAll() { Node<T>? current = head; while (current != null) { Console.Write(current.Value + " "); current = current.Next; } Console.WriteLine(); } }
The Node<T> class holds the value and a reference to the next node.
The LinkedList<T> class manages the nodes starting from head.
// Empty list LinkedList<int> emptyList = new LinkedList<int>(); emptyList.PrintAll(); // prints nothing
// One element LinkedList<string> singleItemList = new LinkedList<string>(); singleItemList.AddFirst("Hello"); singleItemList.PrintAll(); // prints: Hello
// Add at end LinkedList<int> numbers = new LinkedList<int>(); numbers.AddFirst(1); numbers.AddLast(2); numbers.AddLast(3); numbers.PrintAll(); // prints: 1 2 3
// Remove element LinkedList<int> list = new LinkedList<int>(); list.AddLast(10); list.AddLast(20); list.AddLast(30); list.Remove(20); list.PrintAll(); // prints: 10 30
This program creates a linked list of integers. It shows the list when empty, adds items at the start and end, removes an item, and tries to remove a non-existing item. It prints the list after each step.
using System; public class Node<T> { public T Value { get; set; } public Node<T>? Next { get; set; } public Node(T value) { Value = value; Next = null; } } public class LinkedList<T> { private Node<T>? head; public LinkedList() { head = null; } public void AddFirst(T value) { Node<T> newNode = new Node<T>(value); newNode.Next = head; head = newNode; } public void AddLast(T value) { Node<T> newNode = new Node<T>(value); if (head == null) { head = newNode; return; } Node<T> current = head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } public bool Remove(T value) { if (head == null) return false; if (head.Value!.Equals(value)) { head = head.Next; return true; } Node<T>? current = head; while (current.Next != null && !current.Next.Value!.Equals(value)) { current = current.Next; } if (current.Next == null) return false; current.Next = current.Next.Next; return true; } public void PrintAll() { Node<T>? current = head; while (current != null) { Console.Write(current.Value + " "); current = current.Next; } Console.WriteLine(); } } class Program { static void Main() { LinkedList<int> numbers = new LinkedList<int>(); Console.WriteLine("Initial list (empty):"); numbers.PrintAll(); numbers.AddFirst(5); numbers.AddLast(10); numbers.AddLast(15); Console.WriteLine("After adding 5 at start, then 10 and 15 at end:"); numbers.PrintAll(); bool removed = numbers.Remove(10); Console.WriteLine($"Removing 10: {removed}"); numbers.PrintAll(); removed = numbers.Remove(100); Console.WriteLine($"Trying to remove 100 (not in list): {removed}"); numbers.PrintAll(); } }
Adding or removing at the start is very fast (constant time).
Adding at the end takes time proportional to the list length (linear time) if no tail pointer is used.
Removing requires searching, which takes linear time.
Common mistake: forgetting to update the head when removing the first node.
Use linked lists when you need fast insertions/removals and don't need fast random access.
A linked list stores items in nodes linked by references.
You can add items at the start or end, and remove items by value.
Linked lists are good for dynamic collections with frequent changes.