Lesson 16 of 30
Linked lists explained as a treasure hunt: each box holds a value and a clue pointing to the next box, instead of everything sitting in one numbered row.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Think of a treasure hunt. You find a box. Inside it: a small prize, and a clue telling you where the next box is. You walk to that spot, open the next box, find another prize and another clue. You keep going, box to box, until a box says "this is the last one — no more clues."
That's a linked list. Each box (called a "node") holds two things: a value, and a pointer to where the next box lives. You can't jump straight to the fifth box the way you'd jump to the fifth seat in a numbered row — you have to follow the clues from the first box, one at a time, to get there.
Compare that to an array, which you've already met. An array is more like a row of numbered lockers bolted to a wall, all lined up next to each other. If you want locker 5, you walk straight to locker 5 — no clue-following required, because every locker's position is fixed and known ahead of time.
So why would you ever want the treasure-hunt version instead of the numbered-locker version? Because boxes in a treasure hunt don't need to be bolted down. You can slip a whole new box in the middle of the hunt just by changing one clue to point to it — no need to shift every other box down to make room, the way you'd have to shift lockers to squeeze a new one into a full row.
// Each node is one box: a value, and a pointer to the next box (or null if last).
function makeNode(value, next = null) {
return { value, next };
}
// Build the chain backwards: last box first, then link earlier boxes to it.
const third = makeNode("treasure!", null); // last box, no next clue
const second = makeNode("keep going", third); // points to "third"
const first = makeNode("start here", second); // points to "second"
// Walk the chain from the start, following each "next" clue.
function printChain(startNode) {
let current = startNode;
while (current !== null) {
console.log(current.value);
current = current.next; // follow the clue to the next box
}
}
printChain(first);
// "start here"
// "keep going"
// "treasure!"
Notice there's no index anywhere in printChain — no chain[0], chain[1]. The only way to reach a box is to already be holding the box before it, and follow its next clue.
next = null — it's what tells printChain to stop walking instead of trying to follow a clue that doesn't exist.Arrays store everything in one fixed, numbered block, which is what makes jumping to any position instant. Linked lists trade that instant jump for flexibility: because each box only needs to know about the one right after it, boxes can be added or removed anywhere without moving anything else. Same goal — holding a sequence of values — solved with a different tradeoff.