Nodes Chained by Pointers — Dynamic Memory Without Contiguous Storage
Introduction to Linked Lists
Understand what a linked list is, how it differs from arrays, when to choose one over the other, and how JavaScript's garbage collector interacts with node memory.
What you'll learn
- Describe the structure of a linked list node and how nodes form a chain
- Compare linked lists and arrays on access, insertion, and memory layout
- Explain when a linked list is the better choice for a given problem
A linked list is a linear data structure made of individual objects called nodes. Each node holds a value and a reference (pointer) to the next node in the sequence. The list itself only needs to remember where the first node lives — the head — and follows the chain from there.
Node Structure
class ListNode {
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
}
// Build the list 1 → 2 → 3
const head = new ListNode(1, new ListNode(2, new ListNode(3))); Each new ListNode(...) call allocates an object on the JS heap. The
next property is just an object reference — nothing more than a pointer
stored inside a plain JS object.
Linked Lists vs Arrays
Arrays in JavaScript (V8) store elements in a contiguous block of memory when the engine can prove the array is “packed” (all indices filled, same type). That layout makes index access instant but insertions in the middle expensive because every element after the insertion point must shift.
Linked lists scatter nodes anywhere in the heap. There is no shifting — to insert a node you update two pointers. The trade-off is that you cannot jump to the k-th element directly; you must walk the chain.
| Operation | Array (packed) | Linked List |
|---|---|---|
| Access by index | O(1) | O(n) |
| Insert / remove at head | O(n) | O(1) |
| Insert / remove at tail | O(1) amortized | O(n) singly / O(1) doubly |
| Insert / remove in middle | O(n) | O(n) walk + O(1) pointer swap |
| Memory overhead | Low | One extra reference per node |
Memory Layout and JS Garbage Collection
Because each node is an independent heap object, the JS garbage collector
(GC) can reclaim a node the moment nothing else holds a reference to it.
Dropping the head reference of a list lets the GC sweep the entire chain
node by node — no manual free() needed, unlike C.
This also means cache locality is weaker than an array. Nodes may sit far apart in memory, so traversing a linked list causes more CPU cache misses than iterating a packed array of the same length.
When to Choose a Linked List
Linked lists shine when:
- You need frequent insertions or deletions at the front of a sequence.
- You are implementing a queue, stack, or deque with O(1) push/pop at both ends.
- You are building structures like LRU caches or adjacency lists where you need to splice elements in and out without copying.
Arrays win for random access, sorting, and anything that benefits from CPU cache locality or typed-array buffers.
Up Next
Walk through every operation on a singly linked list — insert, remove, and traverse — with full JavaScript implementations.
Singly Linked List →