Stacks, Queues & Heaps
Linked List
🚂
Picture this
A train of carriages stretches down the corridor; you can only walk forward, car to coupled car.
When you see it
Signal words
If the prompt uses any of these, this pattern should come to mind first.
reverse listcyclemiddle nodenth from endmerge listsfast & slow
Ask yourself
- Pointer surgery on nodes (reverse, reorder)?
- Cycle / middle → fast & slow pointers.
- A dummy head simplifies edge cases.
Canonical template
let prev: ListNode | null = null;
let cur = head;
while (cur) {
const next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
Common pitfalls
Losing the rest of the list by reassigning
.nexttoo early.
Null-deref on the final node.