Linked List Foundations

8 concept walkthroughs, each with a worked explanation and an interactive visualization, before you start solving problems in this area.

What is a Linked List?

A linked list is a sequence of nodes where each node stores a value and a pointer to the next node. Unlike arrays, nodes are not stored contiguously in memory. The list starts at HEAD and ends at null.

The intuition

Think of a treasure hunt. Each clue tells you where to find the next clue. You cannot jump directly to clue #5 without following clues 1 -> 2 -> 3 -> 4 first.

When to use it

A real-world analogy

A train where each coach is linked to the next coach. To reach coach 7, you walk through coaches 1 to 6.

Node Anatomy: Value + Next

A node has two core parts: the stored data (value) and a pointer (next) to another node. In singly linked lists, each node only knows its next node.

The intuition

Each node is like a sticky note containing (1) information and (2) directions to the next sticky note.

When to use it

A real-world analogy

A note saying: 'My value is 42. To continue, go to room B-12.'

Traversal: How We Visit Nodes

Traversal means moving from head to null using a current pointer. This is the backbone of almost every linked list algorithm.

The intuition

You walk one stepping stone at a time across a river. Missing one step means you lose the path.

Time complexity

BestO(1) for empty list
AverageO(n)
WorstO(n)

Space complexity: O(1)

When to use it

A real-world analogy

Like reading a chain of emails where each email links to the next one.

curr = head
while curr is not null:
    process(curr.value)
    curr = curr.next

Insert by Rewiring Pointers

To insert a node into a linked list, traverse from head to find the target position, create the new node, point its next to the current next, then rewire the previous node's next to the new node. Two pointer reassignments: that's it.

The intuition

Imagine a chain of paper clips. To add one in the middle, unhook the connection at that spot, clip the new one in, and re-link. No need to shift everything.

Time complexity

BestO(1) if position is known
AverageO(n) to find position
WorstO(n)

Space complexity: O(1)

When to use it

A real-world analogy

Adding a new train car between two existing cars. Just change the couplings, no need to move any other cars.

Delete by Rewiring Pointers

To delete a node, traverse from head to find the node just before the target. Then bypass the target by pointing prev.next directly to target.next. The target node is effectively removed from the chain.

The intuition

Like removing a link from a chain: reconnect the two neighbors and the removed link falls away.

Time complexity

BestO(1) if prev is known
AverageO(n) to find position
WorstO(n)

Space complexity: O(1)

When to use it

A real-world analogy

Removing a broken link from a chain by connecting its two neighbors directly.

Reverse Pattern: prev, curr, next

Reversing a linked list means making every node point backward instead of forward. The tricky part: when you flip node B's pointer from C back to A, you lose access to C forever, because B was your only way to reach C! The solution is three pointers working together: prev (the node we just reversed), curr (the node we're about to flip), and next (a 'bookmark' saving where to go after we flip curr). Before flipping B→A, we first save C in 'next'. Then we safely flip B→A. Then we slide all three pointers one step forward and repeat.

The intuition

Imagine a line of people holding hands: A—B—C. You want them to face the opposite direction. Before person B lets go of C's hand to grab A's hand, someone must remember where C is standing, otherwise C is lost. That 'someone' is the next pointer. It bookmarks C so that after B turns around (grabs A), we can walk to C and repeat the process.

Time complexity

BestO(1) for empty/single node
AverageO(n)
WorstO(n)

Space complexity: O(1)

When to use it

A real-world analogy

Reversing a conga line: each person must first tap the person ahead of them (save next), then turn around to face the person behind (flip pointer), before the next person can do the same. If anyone turns around without tapping first, the line ahead is lost.

prev = null           // nothing before head yet
curr = head            // start at the first node
while curr is not null:  // visit every node once
    next = curr.next   // SAVE bookmark before we break the link!
    curr.next = prev   // FLIP: point backward instead of forward
    prev = curr        // ADVANCE prev one step forward
    curr = next        // ADVANCE curr using our saved bookmark
return prev            // prev is the new head (old tail)

Two Pointers: Slow & Fast

Two pointers moving at different speeds unlock elegant O(n) linked list solutions. Slow moves one step, fast moves two steps. When fast reaches the end, slow is at the middle.

The intuition

Imagine two runners on a track. If one runs twice as fast, by the time the fast runner finishes, the slow runner is exactly at the halfway mark.

Time complexity

BestO(1)
AverageO(n)
WorstO(n)

Space complexity: O(1)

When to use it

A real-world analogy

Two people walking along the same path, one takes double-sized steps. When the fast walker reaches the end, the slow walker is at the midpoint.

Linked List Pattern Playbook

Most linked list problems are combinations of a few reusable patterns: traversal, rewiring, reverse, two pointers, and dummy node.

The intuition

Treat linked list solving like assembling LEGO blocks—small reliable blocks combine into larger solutions.

When to use it

A real-world analogy

A chef's knife skills—once fundamentals are strong, many dishes become easier.

All DSA learning paths