Linked lists, stacks & queues
Pointer surgery (reverse in place, detect cycle), monotonic stacks, deque tricks. Small problems that expose sloppy off-by-one thinking.
The mental model
A linked list is a treasure hunt. Each clue (node) holds a piece of treasure (the value) and tells you where the next clue is (a pointer). You cannot jump to clue number 500 — you must follow the trail from the start, one clue at a time. That is the whole trade: you gave up the array's ability to teleport to any index, and in exchange you got the ability to splice a new clue into the middle of the trail by rewriting two arrows, without moving anything.
Stacks and queues are not new data structures so much as disciplines of access layered on top of a list or array. A stack is a spring-loaded plate dispenser: you can only take the top plate and only add to the top — Last In, First Out. A queue is a line at a ticket counter: you join at the back and leave from the front — First In, First Out. The restriction is the feature: by forbidding random access, they make the right thing the only thing you can do, and that constraint is exactly what matches how certain problems flow.
Why do these exist? Two different reasons. Linked lists exist because arrays are expensive to edit in the middle — every insert or delete shifts O(n) elements — while a linked list splices in O(1) once you are at the spot. Stacks and queues exist because an astonishing number of problems have a natural order of processing: undo/redo, function calls, expression evaluation, backtracking, and depth-first search are all stacks; task scheduling, breadth-first search, and buffering are all queues. The interview payoff is that these are small problems that expose sloppy thinking — reversing a list in place, detecting a cycle, evaluating with a monotonic stack — where one off-by-one or one lost pointer means a crash or an infinite loop. They are where interviewers check whether your pointer manipulation is careful.
The one idea to carry through everything below: a linked list is edited by rewiring pointers, and the order in which you rewire them is the entire game. Rewire in the wrong order and you orphan the rest of the list.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision