Linked Lists in Memory
To really understand why arrays and linked lists behave so differently, it helps to picture the computer's memory: one long row of numbered cells (addresses). Every value your program stores lives at some address.
Arrays: one contiguous block
An array of five integers occupies five neighbouring cells. If it starts at address 1000 and each integer takes 4 bytes, element i is at 1000 + 4 × i. One multiplication and one addition, so access is O(1).
start, size = 1000, 4
for i in range(5):
print(f"a[{i}] lives at address {start + size * i}")a[0] lives at address 1000
a[1] lives at address 1004
a[2] lives at address 1008
a[3] lives at address 1012
a[4] lives at address 1016
The downside: the block must stay contiguous. Inserting at the front means moving every element one cell to the right, and growing past the reserved space means finding a new, bigger block and copying everything over.
Linked lists: nodes anywhere
A linked list's nodes can be scattered across memory. Each node stores its value and the address of the next node. To see this, let's simulate a small memory with a Python list, where an "address" is just an index into it. Each node takes two cells: [value, next_address], and -1 means "no next node".
memory = [None] * 16
def store(address, value, next_address):
memory[address] = value
memory[address + 1] = next_address
# the list 12 -> 7 -> 30, deliberately placed out of order
store(10, 12, 2) # head node at address 10, next node at 2
store(2, 7, 6)
store(6, 30, -1) # last node
head = 10
address = head
while address != -1:
value, nxt = memory[address], memory[address + 1]
print(f"node at {address:>2}: value {value:>2}, next -> {nxt}")
address = nxt
print(memory)node at 10: value 12, next -> 2
node at 2: value 7, next -> 6
node at 6: value 30, next -> -1
[None, None, 7, 6, None, None, 30, -1, None, None, 12, 2, None, None, None, None]
The values are in memory in the order 7, 30, 12, but following the links visits them as 12, 7, 30. The order of a linked list lives in its links, not in its positions.
Inserting without moving anything
To insert 99 after the node holding 12, we put a new node in any free cell and change two links. No existing node moves:
memory = [None] * 16
for address, value, nxt in [(10, 12, 2), (2, 7, 6), (6, 30, -1)]:
memory[address], memory[address + 1] = value, nxt
head = 10
def show(head):
out, a = [], head
while a != -1:
out.append(memory[a])
a = memory[a + 1]
return out
print("before:", show(head))
new = 14 # a free cell pair
memory[new] = 99
memory[new + 1] = memory[10 + 1] # new node points to 12's old next (address 2)
memory[10 + 1] = new # 12 now points to the new node
print("after: ", show(head))
print("cells written: 3")before: [12, 7, 30]
after: [12, 99, 7, 30]
cells written: 3
Compare this with an array, where inserting near the front shifts almost every element.
The costs of scattering
Scattered nodes have real costs that Big O hides:
| Cost | Why |
|---|---|
| Extra memory | every node stores a link (8 bytes on a 64-bit machine) besides its value |
| No random access | to reach node i, follow i links |
| Cache misses | CPUs load memory in chunks; neighbouring array elements arrive together, scattered nodes each need their own trip to RAM |
| Allocation overhead | every node is a separate allocation |
In Python, every object is already a separate allocation with a sizeable header, so a hand-made node is much larger than its value:
import sys
class Node:
__slots__ = ("value", "next")
def __init__(self, value, next=None):
self.value = value
self.next = next
values = list(range(1000))
nodes = [Node(v) for v in values]
print("list of 1000 refs:", sys.getsizeof(values), "bytes")
print("one Node object: ", sys.getsizeof(nodes[0]), "bytes")
print("1000 Node objects:", sum(sys.getsizeof(n) for n in nodes), "bytes")list of 1000 refs: 4028 bytes
one Node object: 24 bytes
1000 Node objects: 24000 bytes
(Pyodide runs 32-bit WebAssembly, so pointers are 4 bytes here; on a 64-bit desktop Python the numbers are about twice as large. Either way, the nodes cost several times more memory than the array of references.)
So which one should I use?
- Need fast access by position, or iterate a lot? Use an array (Python
list). - Constantly insert or delete at the front, or splice in the middle at known nodes? A linked list (or
collections.dequefor both ends). - Not sure? Use an array. Hardware caches make arrays faster in practice for most workloads, even for some operations where linked lists win on paper.
Note: Python variables never hold objects directly; they hold references (addresses). So a Python
listis an array of references, and ourNode.nextis a reference too. That is why the same ideas work in Python as in C, just without visible addresses.
Test yourself
Where are the nodes of a linked list stored in memory?
- Always in one contiguous block
- Anywhere; each node stores the address of the next one
- Only in CPU registers
- In a hash table
Inserting a node after a known node in a linked list requires changing how many links?
- None
- Two (the new node's next and the previous node's next)
- One per node in the list
- All of them
Why are arrays often faster than linked lists in practice, even for traversal?
- Arrays use less CPU
- Contiguous memory makes good use of CPU caches
- Linked lists cannot be traversed
- Arrays are always sorted