Algorithms Playbook
Data structureTime O(n) · Space O(1)8 min read

Linked lists: pointers, reversal and the fast/slow trick

How a linked list stores data, what it's good and bad at compared to an array, and the two pointer techniques behind most linked-list interview problems.

In short

A linked list is a chain of nodes, each pointing to the next. Inserting or removing at a known node is O(1), but reaching the k-th element is O(k). Interview problems are mostly about rewiring pointers carefully.

Arrays vs linked lists

OperationArrayLinked list
Access by indexO(1)O(n)
Insert / delete at the frontO(n)O(1)
Insert / delete after a known nodeO(n)O(1)
MemoryCompact, cache friendlyA pointer per node, scattered

In practice arrays win almost always. Linked lists matter in interviews because they test whether you can manipulate references without losing nodes.

Watch the pointers move

Switch between the two classic algorithms. In reversal the order of the three statements is everything; in fast/slow the speed difference does the work.

Linked lists, pointer by pointer
Algorithm
Speed
∅12345curr
1def reverse(head):
2 prev, curr = None, head
3 while curr:
4 nxt = curr.next
5 curr.next = prev
6 prev, curr = curr, nxt
7 return prev
prev starts as null and curr at the head. We'll walk the list once, flipping each arrow.

Variables

prevnull
curr1
Step 1/22· Steps: 0
The trick is the order: save next, flip the arrow, then move. Reversing a list is also the building block of many harder problems.

The toolbox

  • Dummy head: put a fake node before the head so inserting or deleting the first node needs no special case.
  • Fast and slow pointers: find the middle, detect a cycle (fast catches slow), find the k-th node from the end (move one pointer k steps ahead first).
  • Reverse in place: prev / curr / next. Save next before you overwrite curr.next.
  • Merge two sorted lists: a dummy head and a tail pointer, taking the smaller front each time.

Key takeaways

  • 01Linked lists trade O(1) rewiring for O(n) access.
  • 02Save next before changing a pointer; use a dummy head to avoid special cases.
  • 03Fast/slow pointers find the middle and detect cycles in O(n) time, O(1) space.
  • 04Draw the nodes and arrows; most bugs are visible on paper.

Check your understanding

1 of 3

What's the cost of inserting right after a node you already hold?

Related topics