Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Algorithm · Data Structures

The Chain of Nodes.

Watch the explainer

Don't memorize the linked list — watch the pointers move. Each node holds a value and an arrow to the next one; there's no block of memory, just a chain you follow from the head. Insert at the front in O(1), delete by re-pointing around a node, and reverse the whole thing by flipping every arrow.

nodes · next-pointers · insert · delete · reverse
NODE

A value plus a pointer to the next node. The building block.

HEAD

The pointer to the first node — your only handle on the list.

NEXT

Each node's arrow to the following one; the last points to null.

O(1) INSERT

Re-point a couple of arrows — no shifting, unlike an array.

linked-list.sim — insert · delete · reverse
Ready

A singly linked list: three nodes, each pointing to the next, the last to null. Press Step to run through insert-at-head, insert-at-tail, delete, and reverse — one pointer move at a time.

3
Length
—
Last op cost

Pointers instead of a block

An array stores its elements side by side, so element k is a jump away — but inserting in the middle means shifting everything after it. A linked list gives that up for flexibility: nodes live anywhere in memory, threaded by pointers. To insert or delete you just rewire a couple of arrows — no shifting — but to reach the k-th node you have to walk there from the head.

01

Insert at head

Make the new node's next point at the current head, then move head to the new node. Two pointer writes — O(1).

02

Insert at tail

Walk to the last node and point its next at the new node. O(n) — unless you keep a tail pointer, then O(1).

03

Delete a node

Find the node before the target and re-point its next past the target. The node is unlinked — no gap to fill, no shifting.

04

Reverse

Walk with prev, curr, next and flip each arrow to point backward. The old tail becomes the new head. O(n) time, O(1) space.

Insert / delete at position
O(1)
Access k-th node
O(n)
Search
O(n)
Space
O(n)

Linked lists are the backbone of stacks, queues, adjacency lists and the recency list inside an LRU cache. The cost is the pointer per node and poor cache locality — jumping around memory is slower than scanning a contiguous array — which is why a dynamic array is the better default when you mostly index and iterate.

The picture: flipping one arrow at a time

start1234nullafter 2 steps1234prevcurdone4321head
Reversing [1, 2, 3, 4]. After two steps the first two nodes already point backwards (highlighted) and cur sits on 3, whose pointer has not been touched. Each step flips exactly one arrow, so n nodes need n steps.

Worked example, step by step

Take [1, 2, 3, 4]. Keep three names: prev (the already-reversed part, starting as null), cur (the node being rewired) and nxt (saved before the rewiring so the rest of the list is not lost). Each step does the same four things: save, flip, advance prev, advance cur.

Stepcurnxt (saved)Pointer flippedResult so far
start———reversed: null | rest: 1 → 2 → 3 → 4
1121.next = nullreversed: 1 | rest: 2 → 3 → 4
2232.next = 1reversed: 2 → 1 | rest: 3 → 4
3343.next = 2reversed: 3 → 2 → 1 | rest: 4
44null4.next = 3reversed: 4 → 3 → 2 → 1 | rest: null
donenull—head = prev (4)4 → 3 → 2 → 1

Each row changes one pointer and no node moves in memory, which is why reversal is O(1) extra space. The saved nxt column is what the common first bug skips: write cur.next = prev before saving the old value and the rest of the list is unreachable after step 1. The last row shows that prev, not cur, is the new head: when the loop ends cur is null, one position past the old tail. Four nodes, four flips, no allocation.

Complexity and trade-offs

CaseTimeSpaceWhy
Insert at headO(1)O(1)Two pointer writes. An array insert at index 0 of 1000 elements shifts all 1000 of them.
Insert at tailO(n), or O(1) with a tail pointerO(1)Without a tail pointer you walk the whole list first. Keeping one costs a few lines and one more field to maintain on delete.
Access the k-th nodeO(k)O(1)Reaching the last of 1000 nodes takes 999 pointer hops, where an array does one indexed read.
Delete a known nodeO(1) doubly linked, O(n) singlyO(1)Unlinking is cheap; finding the predecessor in a singly linked list is the walk. A doubly linked node already knows it.
Reverse in placeO(n)O(1) iterative, O(n) recursiveThe recursive form is correct but holds one stack frame per node.

The list wins when you insert and delete near places you already hold a reference to, and loses nearly everywhere else: no indexing, an extra pointer (or two) per node, and nodes scattered across memory so the CPU cache helps far less than it does for an array scan. In practice that means a dynamic array is the default and a linked list is a deliberate choice, usually because other structures hold pointers into it and stable node addresses matter more than speed of traversal.

Common mistakes

  • Overwriting next before saving it

    Writing cur.next = prev and then cur = cur.next walks backwards into the part you already finished. Reversing [1, 2, 3, 4] this way returns [1]: the rest of the list is gone after the first node.

  • Returning the old head

    After a correct loop the old head is the new tail. Returning it (or never assigning head = prev) gives [1] for [1, 2, 3, 4]: a one-node list.

  • Leaving a cycle in the recursive version

    Recursive reversal needs head.next = None after head.next.next = head. Without it the old head and its successor point at each other, and printing the list never ends: [4, 3, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, '…']…

  • Deleting the head with no dummy node

    The usual loop looks for the node before the target, so deleting the head has no predecessor. On [1, 2, 3] deleting the value 1 with that loop raises AttributeError. Start from a dummy node pointing at the head and return dummy.next; the same call then gives [2, 3].

  • Recursing on long lists

    Recursive reversal uses one stack frame per node. In CPython (limit 1000) a list of 5,000 nodes raises RecursionError. Prefer the iterative three-pointer loop for anything user-sized.

Where this shows up

  • LRU caches

    A hash map gives O(1) lookup of a key’s node; a doubly linked list gives O(1) unlinking and re-inserting at the front. Neither structure alone can do both, which is why the pair is the standard LRU design.

  • Hash tables with chaining

    Each bucket is a short linked list of colliding entries. Insert at the head is O(1), and the lists stay short because the table resizes.

  • Allocators and free lists

    A memory allocator can keep freed blocks on a list threaded through the freed memory itself, so taking or returning a block is a couple of pointer writes with no extra storage.

  • Kernels and intrusive lists

    The Linux kernel embeds a doubly linked list node directly inside the structures it manages, so one object can sit on several lists at once and be unlinked in O(1) without a search.

  • Interview staples

    Reverse a list, find the middle with slow and fast pointers, detect a cycle, merge two sorted lists. All of them are the same skill: moving pointers carefully without losing the rest of the chain.

SOLVE IT YOURSELF

Solve it: reverse a linked list

This one is yours to write — in Python or TypeScript, running for real in your browser. Edit the stub below, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are below and Reveal solution is one click away.

YOUR TASK

The scaffold builds a singly linked list from a list of values and flattens it back for checking — your job is the middle part: reverse the list in place by re-pointing each node, using three pointers (prev, cur, next). Set head to the new front. O(n), no extra list.

HINTS — 4 IDEAS
  1. Reversing means every next pointer flips to point at the node before it.
  2. Walk with cur, keeping prev as the node already reversed behind you.
  3. Each step: remember nxt = cur.next, set cur.next = prev, then advance prev = cur and cur = nxt.
  4. When cur falls off the end, prev is the new head — assign it to head.
CPython · WebAssembly
▶  Watch it explained

Reverse a Linked List: Time and Space Complexity, From First Principles

Check yourself

1 · Why is inserting at the head of a linked list O(1)?

Two pointer writes and you're done — nothing after it has to move, unlike an array insert.

2 · What is the cost of reaching the k-th node?

There's no random access; you follow next-pointers from the head, so reaching position k costs k steps.

3 · How do you reverse a singly linked list in O(1) space?

Three references and one pass — flip every arrow to point at the previous node; the old tail becomes the new head.

Questions

Singly vs doubly linked list?

A singly linked list has one pointer per node (next). A doubly linked list adds a prev pointer too, so you can walk backward and delete a node in O(1) given only that node — at the cost of an extra pointer per node. Doubly linked lists power things like LRU caches and browser history.

Why not always use an array?

Arrays win on indexed access and cache-friendly iteration, but inserting or deleting in the middle is O(n) because everything shifts. Linked lists make those edits O(1) at the cost of no random access and extra pointer memory. Pick based on which operations dominate.

How do you detect a cycle in a linked list?

Floyd's tortoise-and-hare: advance one pointer by one node and another by two. If they ever meet, there's a cycle; if the fast one hits null, there isn't. It runs in O(n) time and O(1) space — a classic interview follow-up.

You just rewired a list.

Insert by re-pointing, delete by skipping, reverse by flipping. The pointer-based structure under stacks, queues and caches.

Finished this one? 0 / 99 Algorithms done
NextLIFO & FIFO: Stack & Queue Next on the “The four containers” ladder · rung 2 of 4

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Algorithms