Sub topic 6
Linked lists versus arrays: costs and trade-offs
This chapter assumes you can write a singly linked list with push_front, append, and delete_value, and that you know Big-O notation at an introductory level. Here you compare that structure with an array, using Python's list as the array. Python's list is a dynamic array: it keeps its elements side by side in one block of memory and grows that block when it fills up.
How the two layouts differ
An array stores its elements next to each other. The position of element i is a simple calculation from the start address, so reading items[i] takes one step.
A linked list stores each value in a separate node. Nodes can sit anywhere in memory, and each one holds a reference to the next. To reach the node at position i, you must follow i references from the head.
Cost comparison
| Operation | Array (dynamic) | Singly linked list |
|---|---|---|
| Read by index | O(1) | O(n) |
| Search by value | O(n) | O(n) |
| Insert at front | O(n), elements shift | O(1) |
| Insert at back | O(1) amortized | O(n) with only a head, O(1) with a tail reference |
| Insert after a known node | O(n), elements shift | O(1) |
| Delete at front | O(n) | O(1) |
| Delete a node you already hold | O(n) | O(n) singly, O(1) doubly |
Two entries need explanation.
- Insertion in the middle is O(1) for a linked list only after you have the node. Finding that node still costs O(n) by traversal. The saving is that no elements move.
- "Amortized" means averaged over many operations. Appending to an array is usually cheap, but occasionally the array must copy itself into a larger block. Spread over many appends, the cost per append is constant.
Measuring the front-insert gap
Inserting at the front is where the difference shows most clearly. Python's list.insert(0, x) shifts every existing element, and push_front only creates one node.
import time
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def time_array(n):
items = []
start = time.perf_counter()
for v in range(n):
items.insert(0, v)
return time.perf_counter() - start
def time_linked(n):
head = None
start = time.perf_counter()
for v in range(n):
head = Node(v, head)
return time.perf_counter() - start
for n in (10_000, 20_000, 40_000):
print(n, round(time_array(n), 4), round(time_linked(n), 4))When n doubles, the array time grows by roughly four times, because each of the n inserts shifts up to n elements. The linked time only doubles. Exact numbers depend on your machine.
Costs the table hides
Big-O does not capture everything.
- Memory per element. Each node stores a reference in addition to its value. In Python each node is also a full object with its own overhead, so a linked list uses noticeably more memory than a list of the same values.
- Cache behavior. Contiguous elements load into the processor's cache together, so scanning an array is fast. Nodes scattered through memory cause more cache misses. For small and medium sizes, an array often wins at traversal even when Big-O says the two are equal.
- Allocation. Every insertion allocates one node, while an array allocates in occasional large blocks.
Choosing between them
Prefer an array when you need random access by index, when you mostly append and iterate, or when memory and speed on small data matter. Prefer a linked list when you add or remove at the front often, when you hold references to nodes and splice at those positions, or when you need to avoid copying a large block on growth.
Exercise
A program stores 1,000,000 numbers and reads them by index thousands of times, but rarely inserts. Which structure fits, and why? Then name one workload where a linked list would be the better choice.
Show answer
An array fits, because indexed reads are O(1) in an array and O(n) in a linked list, and arrays also use less memory per element and scan faster through the cache. A linked list is better when the program repeatedly inserts or removes at the front, such as a stack of recent items, because each of those operations is O(1) and no elements are shifted.