Learn linked list interview patterns with pointer animations for reversal, cycle detection and LRU cache problems. Explore explanations and Python solutions.
The pattern
Linked List: it's all about re-pointing arrows
A linked list is a chain of nodes, each holding a value and a pointer to the next node. You can't index into it, so every problem is about walking it with a few well-placed pointers and changing arrows in the right order. Draw the arrows, decide which ones change, and save anything you're about to lose.
1Arrows, not positions
Each node only knows the next one. To insert 9 between 2 and 5, change two arrows; nothing shifts. But there's no jumping to the middle: you have to walk there.
2Flip arrows one at a time
Reversing walks the list with prev and curr. Save curr.next, point curr back at prev, then step both forward. Reorder List and Reverse Nodes in K-Group build on this.
3Fast and slow pointers
Move one pointer 1 step and another 2 steps. When fast reaches the end, slow is in the middle. If there's a loop, fast laps slow and they meet.
How to spot it (and the four tricks)
- Dummy node before the head, whenever the head might change (merge, remove, reverse in groups).
- Fast and slow pointers for the middle, a cycle, or “n-th from the end” (a fixed gap).
- Reverse in place with prev and curr, often just part of the list.
- Hash map of nodes for copying (old → new) or for O(1) lookup next to a list (LRU Cache).
Reading the pictures
- Boxes are nodes and arrows are next pointers; ∅ is None. Orange labels under a node are the pointers on it (prev, curr, slow …).
- Arrows that skip over nodes curve above the row; arrows pointing backwards curve below. Dashed arrows are random pointers.
- New to linked lists? Try the Linked List visualizer first. Floyd's algorithm has its own page too: the tortoise and the hare.
NeetCode 150 · Linked List
All 11 problems
Pick a problem to see it solved step by step. They're in NeetCode's order, roughly easiest first.
Reverse Linked List
EasyBlind 75Given the head of a singly linked list, reverse it and return the new head.
head = 1 → 2 → 3 → 4 → 5
the list (arrows are next pointers)
variables
- prev
- None
- curr
- 1
Press Play or Step to watch the solution run.
Brute force vs the pattern
On this input:
- Pattern: O(n)
- 5 ops
- Brute force
- n/a
Copying the values into an array and writing them back reversed also takes O(n), but needs O(n) extra memory, and it changes values rather than links. Flipping the arrows needs only two pointers.
“ops” counts basic steps: lookups, inserts, comparisons, array reads. On tiny inputs brute force can even win, because it may stumble on the answer early; as n grows, the gap opens up.
How it works: Reverse Linked List
Step by step
- curr
- Arrow already flipped
Press Start or Step: the step the animation is on lights up below.
- 1
prev = None, curr = head
Nothing is flipped yet.
prev, curr = None, head - 2
Save next, flip, step forward
curr.next = prev, then prev = curr, curr = next.
nxt = curr.next; curr.next = prev; prev, curr = curr, nxt - 3
curr is None → return prev
prev is the new head.
return prev
Each node only knows the node after it. To reverse the list, every arrow must point the other way, so visit each node once and flip its arrow to point at the node before it.
The catch: once you flip curr.next, you've lost the way forward. So save next first, then flip, then step prev and curr forward. When curr runs off the end, prev is the old tail, which is the new head.
Spot it in an interview
- Any time part of a list must be reversed: it's a building block for Reorder List, Palindrome List and Reverse Nodes in k-Group.
- The recursive version is shorter but uses O(n) call stack.
One visit per node, and only three pointers.
Credits
Learn more from NeetCode
The problem list and order come from the NeetCode 150 by NeetCode (neetcode.io). The solutions here follow the approaches NeetCode teaches in the linked videos; the animations, explanations and code were written for CodeQuest. If these help, NeetCode's videos and site are well worth your time.
Problem statements are summarised in our own words; the full versions are on LeetCode.