Linked List
A linked list is a chain of nodes, each holding a value and a reference to the next node (and sometimes the previous). Insert and delete at a known node are O(1); indexing is O(n).
Detailed Explanation
JavaScript arrays are the default list. They are contiguous (when packed), indexable in O(1), and they shift their tail on middle inserts. A linked list exists for the cases where you already hold a node and want to splice without moving the rest, or where you need O(1) add/remove at both ends without a ring buffer.
A singly linked node is { value, next }. The list is the reference to the head. Walking is for (let n = head; n; n = n.next). There is no list[i] except by walking i steps. A doubly linked node also has prev, so you can walk backwards and delete a node without finding its predecessor.
The operations:
| Operation | Singly, at head | Singly, at tail (no tail pointer) | Doubly, given node |
|---|---|---|---|
| insert | O(1) | O(n) | O(1) |
| delete | O(1) | O(n) | O(1) |
| find by value | O(n) | O(n) | O(n) |
index i | O(i) | O(i) | O(i) |
Keep a tail pointer if you enqueue at the end. Keep head and tail for a deque. Watch the empty-list cases: inserting the first node and deleting the last node both have to update those pointers to null.
There is no built-in list. You write the class, or you use an array. In application code, the array almost always wins: cache locality, slice, map, JSON. In interviews, lists show up as "reverse a list", "detect a cycle" (Floyd), "merge two sorted lists", and "LRU cache" (map plus doubly linked list).
A cycle happens when some next points at an earlier node. JSON.stringify and a naive while (n) then hang. Detect with a Set of visited nodes or with two pointers (slow + fast).
What it costs you: pointer chasing, extra objects, and no random access. Each node is a heap allocation. Walking a million nodes is slower than walking a million-element packed array. Prefer arrays unless you have a measured reason or an algorithm that is naturally node-based.
Reach for a linked list when you need O(1) splice at a held node, or when teaching / implementing LRU, queues with middle cancel, or explicit graphs of "next step". Reach for an array for everything else.
Code Examples
function node(value, next = null) {
return { value, next };
}
function fromArray(values) {
let head = null;
for (let i = values.length - 1; i >= 0; i -= 1) {
head = node(values[i], head);
}
return head;
}
function toArray(head) {
const out = [];
for (let n = head; n; n = n.next) out.push(n.value);
return out;
}
const list = fromArray([1, 2, 3]);
console.log(toArray(list)); // => [ 1, 2, 3 ]function reverse(head) {
let prev = null;
let curr = head;
while (curr) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
console.log(toArray(reverse(fromArray([1, 2, 3])))); // => [ 3, 2, 1 ]function hasCycle(head) {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
console.log(hasCycle(fromArray([1, 2, 3]))); // => falseCommon Gotchas
A cycle makes a walk infinite
always have a termination argument (null `next`, or a visited set, or two pointers) before you loop on `n.next`.
Losing `head` loses the list
unlike an array variable, there is no other handle unless you stored every node.
Arrays are usually faster
do not replace `[]` with a list for a stack or a small queue.
`JSON.stringify` on a cyclic list throws
the same `TypeError` as any circular object.