Linked Lists - potatoscript/dsa GitHub Wiki
π§© What is a Linked List?
Imagine a chain of paper notes π.
Each note has:
- π¦ A piece of data (like a name)
- β‘οΈ A pointer to the next note
Together, they form a chain β thatβs a Linked List!
πΌοΈ Visual Example:
[π§Έ Tom ] β‘οΈ [πΆ Max ] β‘οΈ [π± Leo ] β‘οΈ null
Each box is called a Node:
- It stores something (like βTomβ)
- And knows where the next one is!
π§ Why Use a Linked List?
Compared to an array:
- β Easier to grow and shrink
- β Insert/delete from the middle? No problem!
- β But slower to access by index
π§ Real-World Analogy
Situation | Linked List Comparison |
---|---|
πββοΈ People holding hands | Each person knows who's next |
π Train cars connected | Each car links to the next |
π¬ Letters with directions | βGo to box 2β written on each |
π» C# Code: Simple Linked List
Define a Node:
public class Node
{
public string data;
public Node next;
public Node(string value)
{
data = value;
next = null;
}
}
Create and Connect Nodes:
Node first = new Node("Tom");
Node second = new Node("Max");
Node third = new Node("Leo");
first.next = second;
second.next = third;
π§ How Does It Work?
You only need the first node (called the head), then you can follow the links.
first β second β third
To get all names:
Node current = first;
while (current != null)
{
Console.WriteLine(current.data);
current = current.next;
}
π§ͺ Array vs Linked List
Feature | Array | Linked List |
---|---|---|
π’ Access by index | Fast (O(1)) | Slow (O(n)) |
β Insert/delete | Slow (O(n)) | Fast (O(1) if at head) |
π Size | Fixed | Dynamic |
π§ Memory use | Compact | More memory (extra pointers) |
π§© Parts of a Linked List
A basic singly linked list node has:
data
: the value (e.g., "Dog")next
: the link to the next node
There are other types too!
π Types of Linked Lists
Type | Description |
---|---|
Singly Linked List | One-way chain β |
Doubly Linked List | Two-way chain β |
Circular Linked List | Loops around π |
π§΅ Singly Linked List
[ A ] β [ B ] β [ C ] β null
π§· Doubly Linked List
null β [ A ] β [ B ] β [ C ] β null
π Circular Linked List
[ A ] β [ B ] β [ C ]
β β
βββββββββββββββββ
π Big O Notation
Operation | Time Complexity |
---|---|
Insert at head | O(1) |
Insert at end | O(n) |
Delete at head | O(1) |
Delete at end | O(n) |
Access by index | O(n) |
Search | O(n) |
π Fun With Code: Traverse a List
Node current = head;
while (current != null)
{
Console.WriteLine("π§‘ " + current.data);
current = current.next;
}
π¨ Insert at the Beginning
Node newHead = new Node("Newbie");
newHead.next = head;
head = newHead;
π§ Insert at the End
Node current = head;
while (current.next != null)
{
current = current.next;
}
current.next = new Node("LastOne");
ποΈ Delete a Node
Delete head:
head = head.next;
Delete specific:
Node current = head;
while (current.next != null)
{
if (current.next.data == "Max")
{
current.next = current.next.next;
break;
}
current = current.next;
}
π¨ Pitfalls to Avoid
Mistake | What Happens |
---|---|
β Breaking the chain | You lose the rest of the list |
β Loop with null check | NullReferenceException! |
β Infinite loop | You forgot to move to next ! |
π©βπ« Summary Time!
β
Linked Lists are made of nodes
β
Each node has a value and a pointer
β
Good for adding/removing items
β
Slower to search/access by position
β
Many types: singly, doubly, circular