Follow References From Node to Node

An array keeps its items side by side, so items[500] jumps straight to the item you want. A linked list gives that up. Each item lives in its own node, and the node holds a reference to the next one. Getting to item 500 means starting from the head, the first node, and hopping along 499 links.

That sounds like a bad trade, and for random access it is. But relinking is cheap. Inserting or removing a node in the middle of a list only changes a reference or two, while splice() on an array shifts everything after the change. Linked structures also show up whenever data points to other data, such as a chain of redirects or an undo history.

Nodes Are Just Objects

JavaScript has no built-in linked list, and you don’t need a class to build one. A plain object with a next property is a node, and null marks the end:

Language: JavaScript
const third = { song: "Outro", next: null };
const second = { song: "Bridge", next: third };
const head = { song: "Intro", next: second };

let count = 0;
for (let node = head; node !== null; node = node.next) count++;
console.log(count);

The loop follows next until it runs off the end of the list:

Language: Program Output
3

You’ll write this walk in almost every linked-list problem. It visits each node once, so it takes O(n) time.

Two Runners at Different Speeds

Some questions about a list seem to need its length first. Where’s the middle? Does the list loop back on itself? You can answer both with two references that walk at different speeds.

When the fast one moves two nodes for every one node the slow one moves, the slow one is halfway along when the fast one reaches the end. If the list contains a loop, the fast one never reaches the end. Instead, it eventually catches up with the slow one from behind. Either way, you use O(1) extra space, because you only hold two references.

JavaScript Details That Interviewers Notice

Compare nodes with ===, not by their contents. Two objects are equal only when they’re the same object, even if every property matches:

Language: JavaScript
> const a = { song: "Intro", next: null };
undefined
> const b = { song: "Intro", next: null };
undefined
> a === b
false
> a === a
true

That’s exactly what you want in list problems. A loop means you came back to the same node, not to a node with the same data, and a Set of node objects tracks identity the same way.

Keep null and undefined apart. The list ends at null, but a variable you declare without a value starts as undefined. A loose check like while (node) treats both as the end, which is convenient, but code that writes a reference back into a node should store null on purpose. If you reverse a list starting from let previous;, the new last node ends up with next: undefined, and a caller that looks for node.next === null never finds the end.

Relinking changes the caller’s list. A function that receives a node gets a reference to the same object, not a copy. When your function sets node.next = something, every other piece of code that holds that node sees the change. Sometimes that’s the point, as in an in-place reversal. Other times, the list must stay untouched, so keep bookkeeping such as visited nodes in your own Set instead of adding properties to the nodes.

Detach what you remove. Unlinking a node from a list doesn’t erase the node. If something else still holds it, its next still points into the list and keeps the nodes after it reachable, so the garbage collector can’t free them. Setting the removed node’s next to null cuts that tie.

In this section, you’ll walk a play queue, reverse an undo history in place, remove an item counted from the end, and detect a loop in a chain of redirects. Before you write any code, draw the arrows for a three-node list and redraw them after each step. Most linked-list bugs show up on paper before they show up in tests.

Become a Member to join the conversation.