A close-up of a heavy metal chain on a stone surface. Photo · henry perks / Unsplash
A linked list is exactly this: each node points to the next. Beloved of textbooks; usually wrong in real code.

You can store a sequence of values two ways: in a row (an array — Week 29) or in a chain (a linked list, this week). They have the same logical capability — keep N values, walk through them, add and remove — but very different physical layouts and very different performance characteristics.

Linked lists are the canonical example for teaching pointers, and the canonical example of how textbooks lie about real-world performance. By the end of today you'll know what they are, when to use them, and why you almost never should.

The shape

A linked list is a sequence of nodes. Each node holds a value and a pointer to the next node. The list itself is a pointer to the first node (the "head"). The last node's "next" pointer is null.

struct Node {
    int    value;
    Node*  next;
};

Node* head = new Node{10, nullptr};
head->next = new Node{20, nullptr};
head->next->next = new Node{30, nullptr};

// to walk the list:
for (Node* n = head; n; n = n->next) {
    std::cout << n->value;
}

Compare to an array: int arr[] = {10, 20, 30}. Same data, but the array packs them in 12 contiguous bytes, while the linked list scatters them across the heap, each node 16+ bytes (value + pointer + allocator overhead).

The trade-off

Linked lists are theoretically better at:

Linked lists are dramatically worse at almost everything else:

The textbook says "use a linked list if you do lots of inserts/deletes". Reality says "even then, a vector is usually faster up to several thousand elements, because cache effects swamp the algorithmic advantage". The classic advice is wrong on modern hardware.

When to actually reach for one

For everything else: std::vector.

Doubly linked, circular, and intrusive

Variations exist:

Why this matters for AI

Almost nowhere — and that's the point. AI workloads are vector- and matrix-heavy, and contiguous memory access is everything. You'd struggle to find a linked list in any modern AI framework's hot path. Where they appear at all is in metadata structures (the operator dependency graph, the training callback chain) where O(1) splicing is occasionally useful.

Linked lists: famous, often wrong. Reach for a vector first.

Try it yourself

What's next

Two simple data structures with specific access patterns — and powerful applications.

Week 35 is Stacks & Queues.

Photo credit

Photo free under the Unsplash license. Chain · henry perks.

Quick check

1. What does a singly-linked-list node hold?
  1. Just a value
  2. A value and a pointer to the next node
  3. A value and pointers to all other nodes
Reveal Answer

Answer: B. Doubly-linked lists add a 'previous' pointer. Both pay an indirection per step.

2. What is the cost of inserting at the head of a linked list?
  1. O(N)
  2. O(1)
  3. O(log N)
Reveal Answer

Answer: B. Constant time. That's the linked list's main advantage over a vector — no shifting elements down.

3. Why is iterating a linked list slow on modern hardware?
  1. Pointer arithmetic is expensive
  2. Each node lives at an unrelated address — every step is a likely cache miss
  3. Linked lists use 64-bit ints
Reveal Answer

Answer: B. The cache line you just loaded contains nothing useful for the next step. This is why std::list is rarely a good choice.