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.

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.

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.

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.

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.

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.

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.

Linked List Pattern Playbook

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

All DSA learning paths