Linked Lists
Nodes joined by next pointers - cheap to relink, slow to index.
- visited / done
- result
Start
prev is None and curr is node 1. Nothing is reversed yet.
VARIABLES
- prev
- None
- curr
- 1
- reversed part
- -
Intuition & complexity
The core mental model, trade-offs, and Big-O cost.
What it is
Each node holds a value and a reference to the next node. Reaching position i means walking i steps from the head.
Why it matters
Inserting or removing next to a node you already hold is O(1) - nothing has to shift.
Think of it like this
A treasure hunt: each clue tells you where the next one is hidden.
Use it when
- Frequent inserts or deletes once you hold the node
- Building queues, LRU caches or adjacency lists
- Problems that hand you a ListNode
Avoid it when
- You need random access by index
- Memory is tight - every node carries pointer overhead
Complexity cheatsheet
| Operation | Time | Space |
|---|---|---|
| Access node i | O(n) | O(1) |
| Insert or delete after a known node | O(1) | O(1) |
| Reverse in place | O(n) | O(1) |
Often confused with: arrays - O(1) indexing, but O(n) inserts in the middle.
How to recognize it
Clues in interview prompts that point straight to this pattern.
- O(1) extra space on a list means re-pointing nodes, not copying values
- A dummy head node removes special cases at the front
Mistakes & edge cases
Catch common off-by-one bugs and corner cases before submitting.
Common mistakes
- Overwriting curr.next before saving it - the rest of the list is lost
- Forgetting to return the new head
- Not handling an empty list
Edge cases to test
- Empty list (head is None)
- A single node
- A list with a cycle - reversal never ends
Knowledge check
Quick check to lock in the intuition.
During reversal, why save nxt = curr.next before anything else?
Practice
Apply what you just watched on real interview problems.