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