Log inStart free
LESSON 3.01 · 5 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Linked lists

Watch a chain of nodes grow and shrink by rewriting a few pointers.


Pro lesson. Stage 1 is free to watch. Pro opens all 5 stages, 3 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 3.01 · POINTER BAY6 NODES 288 B · LIST 104 B
01
Stage 1 of 5 · Nodes and pointers
A linked list keeps each value in a node. The node's pointer leads to the next node.
Explain it simply

Each box on the top row is a node: a value plus a pointer, the id of the next node. The nodes sit anywhere in memory, so their ids jump around. The Python list below keeps its items side by side, 8 bytes apart.

A singly linked list of 6 nodes holds 3, 8, 1, 6, 4, 9. head points to node id 2720. The last next pointer is None. Below, a Python list holds the same values in slots 8 bytes apart, from address 7496.

valuepointer, id, addresswalk, new nodeitem movedNone, unlinked
linked listll = LinkedList([3, 8, 1, 6, 4, 9])python lista = [3, 8, 1, 6, 4, 9]costpython listlinked listhead3id 2720[0]8id 3584[1]1id 3200[2]6id 4256[3]4id 3440[4]9id 3632[5]Nonea[0]7496[1]7504[2]7512[3]7520[4]7528[5]7536381649memory104 B288 Blinked listll = LinkedList([3, 8, 1, 6, 4, 9])python lista = [3, 8, 1, 6, 4, 9]costpython listlinked listhead3id 2720[0]8id 3584[1]1id 3200[2]6id 4256[3]4id 3440[4]9id 3632[5]Nonea[0]7496[1]7504[2]7512[3]7520[4]7528[5]7536381649memory104 B288 B
Toy: 4 to 8 nodes · one inserted value · ids are short stand-ins for addresses
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
node=(value, next),addr(a[i])=base+8i\text{node} = (\sy{val}{data}{\text{value}},\ \sy{next}{param}{\text{next}}),\quad \sy{addr}{param}{\text{addr}}(a[i]) = \text{base} + 8i
node2720=(3, 3584),addr(a[1])=7496+8⋅1=7504\text{node}_{2720} = (3,\ 3584),\quad \text{addr}(a[1]) = 7496 + 8 \cdot 1 = 7504

A node holds a value and a pointer; list slots sit 8 bytes apart.

Common mix-ups

MYTHA linked list reads item i in one step, like a list.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01How many hops does the walk to the last of 6 nodes take?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 5
Unlock the full explainer
  • 4 more stages and 3 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.