Essential Data Structures home
Linked List
A chain of nodes, each holding a value and a pointer to the next node. The items sit wherever they fit in memory, and the links, not the positions, hold the order together.
What it is
A linked list is a sequence built from nodes. Each node holds a value and a pointer to the next node in the chain. A single pointer, the head, points to the first node. The last node's pointer is empty, which marks the end of the list.
Unlike an array, the nodes do not sit side by side. Each node is allocated on its own, wherever memory happens to be free. That is the whole trade: the list can grow and shrink one node at a time without ever moving the other items, but there is no arithmetic that jumps straight to the tenth node. To reach it, you walk the chain from the head, one pointer at a time.
There are two common variations. A doubly linked list gives each node a second pointer to the previous node, so the list can be walked in either direction and a node can be removed when you already hold it. A circular list points the last node back to the head, so the chain has no end, which suits round-robin schedules.
How it works
Adding at the head is a single step: make a new node, point it at the old head, and move the head pointer to the new node. Removing the head is just as quick: move the head pointer to the second node. Nothing else moves, and nothing is copied.
Inserting or deleting elsewhere is cheap only once you are already there. If you hold the node before the spot, you change two pointers and the job is done. But finding that spot means walking the chain from the head, and that walk takes time proportional to the number of nodes passed. Adding at the tail without a tail pointer is the same story: you walk to the end first. Keeping a tail pointer makes appending constant time, at the cost of one extra pointer to maintain.
That is why lists are good at frequent inserts and deletes in the middle, once the position is known, and bad at random access. There is no index, only a chain.
Complexity
| Operation | Average | Worst case |
|---|---|---|
| Access by index | O(n) | O(n) |
| Search by value | O(n) | O(n) |
| Insert or delete at the head | O(1) | O(1) |
| Insert or delete at a known node | O(1) | O(1) |
| Insert or delete at the tail | O(n) | O(n) |
| Insert or delete at the tail, with a tail pointer | O(1) | O(1) |
n is the number of nodes. Inserting or deleting in the middle costs O(1) once the position is found, but finding it costs O(n).
When to use it, and when not to
Use a linked list when the size changes often and unpredictably, when items are added and removed at the front, or when you already hold a node and need to splice something in next to it. That is the shape of an undo history, a playlist being reordered, or the free blocks in a memory allocator.
Do not use a linked list when you need to reach the tenth or the ten-thousandth item directly, or when you mostly scan the whole collection in order. An array does both faster, because its items sit together and the processor can fetch the next ones before they are asked for. A linked list also spends memory on one pointer per node, which matters when the values are small.
Where you meet it
Stacks and queues are often built on linked lists, and so are hash tables: each bucket holds a short chain of entries that collided on the same hash. A browser's history, an operating system's list of running processes, and the chain of free memory blocks are all linked lists. Python's collections.deque is a doubly linked list of blocks under the hood.
In Python
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
head = Node(19) # start with one node
head = Node(7, head) # insert 7 at the head, O(1)
head = Node(12, head) # insert 12 at the head, O(1)
node = head
while node is not None: # walk the chain, O(n)
print(node.value, end=" ")
node = node.next
# prints: 12 7 19
In short: a linked list trades instant access for cheap changes. Pointers replace positions, so adding and removing are easy, and reaching an item means walking the chain.
Sources
- Linked list, Wikipedia
- Python tutorial, Data Structures (lists and deques)
- Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, chapter 10