What is a Linked List
A linked list is a chain of nodes scattered across memory, where each node holds a value and a pointer to the next node. Unlike the elements of an array, the nodes aren't neighbors and can live anywhere in RAM. The pointers are what keep them together.
struct Node { int value; struct Node *next;};A node has two fields: the data you care about and the address of the node that comes next.
The treasure hunt analogy
Imagine a treasure hunt where each clue tells you where to find the next one. You start at a known location (the "head"), read the clue, then go wherever it points. You keep following clues until you find one that says "end here", which is NULL.
A linked list works the same way. You start at the head pointer, follow the chain of next pointers, and stop when you hit NULL.
head → 42 → 17 → 8 → NULL
The problem arrays couldn't solve
In the Arrays article, we saw that inserting into the middle of an array forces every following element to shift right. For a million elements, that's a million moves, for what feels like a simple operation.
Linked lists avoid this. Instead of moving data around, you redirect pointers.
Inserting without shifting
Imagine a line of people where each person holds the next person's hand. To add someone in the middle, you don't shuffle everyone; you break two hands and form two new ones. The newcomer grabs the person ahead of them, and the person behind grabs the newcomer.
That's pointer surgery: nobody moves, and the list is rearranged by changing which addresses are stored.
This comes at a cost. Arrays guarantee instant access to any element by index, and linked lists give that up in exchange for cheap insertion and deletion.
How nodes connect
A head pointer anchors the list: a variable that stores the address of the first node. The last node's next field is NULL, which marks the end of the chain.
struct Node *head = NULL; // Empty listTo build a list of three nodes:
struct Node *a = malloc(sizeof(struct Node));struct Node *b = malloc(sizeof(struct Node));struct Node *c = malloc(sizeof(struct Node));a->value = 42; a->next = b;b->value = 17; b->next = c;c->value = 8; c->next = NULL;head = a;Now head → 42 → 17 → 8 → NULL.
Traversal: walking the chain
Because nodes aren't stored contiguously, you can't jump to an arbitrary position. There's no calculation like start + (index × size). To reach the 5th element, you must walk through the first four.
struct Node *current = head;while (current != NULL) { printf("%d\n", current->value); current = current->next;}This is for every access by position. The scattered layout that makes insertion cheap is also what makes random access expensive.
Insertion: where Linked Lists shine
Insertion has three distinct cases, each with different costs.
At the head
This is the cheapest insertion: make the new node point to the current head, then update head to the new node, which takes two pointer assignments.
struct Node *new_node = malloc(sizeof(struct Node));new_node->value = 99;new_node->next = head;head = new_node;Before: 42 → 17 · After: 99 → 42 → 17
This is , regardless of list length.
After a known node
If you already hold a pointer to a node, inserting immediately after it is also O(1). Splice the new node in:
// Insert new_node after 'target'new_node->next = target->next;target->next = new_node;Order matters here. If you set target->next = new_node first, you lose the reference to the rest of the list.
At the tail (or arbitrary position)
Without a tail pointer, reaching the last node requires traversing the whole list, which is O(n). The insertion itself is O(1), but finding the right position costs O(n).
Many linked list implementations maintain a separate tail pointer alongside head, so appending stays O(1).
Deletion: pointer surgery
Deleting a node means making its predecessor skip over it, then freeing the memory.
// Delete the node after 'prev'struct Node *to_delete = prev->next;prev->next = to_delete->next;free(to_delete);Before: 42 → 17 → 8 · After: 42 → 8
Deleting a specific value takes O(n) to find the node, then O(1) to unlink it. Deleting the head is always O(1):
struct Node *old_head = head;head = head->next;free(old_head);Try it yourself
Insert a value at index 2 and watch the order of the writes: the new node points at the rest of the list first, and only then does the node before it let go. Then press Insert at tail. This list has no tail pointer, so it walks every node to get there, and the walk is the O(n). The addresses above the nodes are scattered on purpose: unlike an array, nothing sits next to anything.
Keyboard and mouse
- Space
- play or pause
- ←→
- previous or next step
- HomeEnd
- start or end of the run
- <>
- slower or faster
- M
- sound on or off
- F
- fullscreen
- R
- reset
- Space+drag
- move the canvas
- Ctrl+scroll
- zoom
- every step of the next run shows up here
Common operations and their costs
Most operations depend on where the node is. If it's the head, the list answers right away; if it's the tail, the list has to walk past every other node first. The table shows both ends of that range:
| Operation | Best | Worst | Why |
|---|---|---|---|
| Access by index | O(1) | O(n) | Index 0 is the head; any other index means walking from it |
| Search by value | O(1) | O(n) | The value may be at the head, or at the tail, or missing |
| Insert at head | O(1) | O(1) | Just redirect the head pointer |
Insert at position i | O(1) | O(n) | Walk to node i − 1, then two pointer updates |
| Insert at tail (no tail ptr) | O(n) | O(n) | Always walks the whole list to find the tail |
| Insert at tail (with tail ptr) | O(1) | O(1) | Tail pointer gives direct access |
| Insert after known node | O(1) | O(1) | Two pointer updates |
| Delete at head | O(1) | O(1) | Redirect head, free node |
| Delete by value | O(1) | O(n) | Find the node and its predecessor, then O(1) to unlink |
The best case only happens when the node you need is the head, so it says little about a list in general. When people say a linked list has O(n) access, they mean the worst column, and that's the one to plan around.
Linked Lists vs Arrays
Put next to the Arrays table, the two structures fail in opposite places:
| Operation | Array | Linked list (singly) |
|---|---|---|
| Access by index | O(1) | O(n) |
| Search by value (unsorted) | O(n) | O(n) |
| Insert or delete at the front | O(n) | O(1) |
| Insert at the end | O(1) (amortized) | O(n), O(1) with a tail ptr |
| Delete at the end | O(1) | O(n), even with a tail ptr |
| Insert or delete in the middle | O(n) to shift | O(n) to walk, then O(1) |
| Extra memory per element | None | One pointer (8 bytes on 64-bit) |
| Memory layout | Contiguous, cache-friendly | Scattered, a possible cache miss per node |
Finding a position costs O(n) in a list, but the insert or delete once you're there is O(1). Arrays have the opposite: finding by index is O(1), but changing the structure costs O(n). In the middle both end up at O(n), for different reasons: the array moves the elements after the position, and the list walks the nodes before it. Deleting the last node stays O(n) even with a tail pointer, because the list has to find the node before the tail to set its next to NULL.
The playground runs the same operation on both structures at once. Choose an operation, then press Front and End: inserting at the front takes the array 9 steps and the list 1, and inserting at the end swaps those numbers. Access shows the other side of the trade, with the array at 1 step for every index.
Keyboard and mouse
- Space
- play or pause
- ←→
- previous or next step
- HomeEnd
- start or end of the run
- <>
- slower or faster
- M
- sound on or off
- I
- open the input
- F
- fullscreen
- R
- reset
- Space+drag
- move the canvas
- Ctrl+scroll
- zoom
- every step of the next run shows up here
- every step of the next run shows up here
O(n)O(1)The comparison appears when both finish.
Doubly Linked Lists
A singly linked list only knows how to go forward. A doubly linked list adds a prev pointer, letting you walk in both directions.
struct DNode { int value; struct DNode *next; struct DNode *prev;};NULL ← 42 ⇄ 17 ⇄ 8 → NULL
If you hold a direct reference to a node, you can delete it in O(1). You don't have to walk from the head to find its predecessor, because the node already has it in prev.
In exchange, each node carries two pointers instead of one, which on a 64-bit system is 16 bytes of overhead per node. Every insert or delete also has to update both next and prev correctly, so there are more steps and more places for a bug.
| Operation | Singly | Doubly |
|---|---|---|
| Pointers per node | 1 (next) | 2 (next and prev) |
| Pointer overhead per node (64-bit) | 8 bytes | 16 bytes |
| Walk backwards | Not possible | O(n) |
| Delete a node you hold a reference to | O(n) | O(1) |
| Insert before a known node | O(n) | O(1) |
| Delete the tail (with a tail ptr) | O(n) | O(1) |
Every O(n) in the singly column comes from the same problem: the operation needs the previous node, and a singly linked list can only find it by walking from the head.
Use doubly linked lists when you need to traverse backwards or delete nodes you have direct references to. Use singly linked lists when forward traversal is enough and memory is tight.
Circular Linked Lists
In a circular linked list, the last node's next points back to the head instead of NULL. There is no end, so the list loops forever.
// Making a 3-node list circularc->next = head; // c was pointing to NULLIt maps naturally to cyclic processes: a media player looping through a playlist, a round-robin scheduler cycling through tasks, or a buffer where the "end" wraps around to the "beginning".
When traversing a circular list, be careful: without tracking when you've looped back to the start, you'll iterate forever.
Linked Lists in the real world
Linked lists sit underneath many familiar abstractions:
- Browser history: doubly linked, so the back and forward buttons traverse in both directions
- Undo/redo in editors: each state is a node; undo walks
prev, redo walksnext - LRU caches: a doubly linked list paired with a hash map gives O(1) cache evictions
- Stacks and queues: both are simple to implement on top of a linked list
- Operating system memory allocators: free memory blocks are tracked with linked lists
In standard libraries: std::list in C++, LinkedList in Java, and collections.deque in Python are all backed by doubly linked list implementations.
When Linked Lists fall short
Linked lists are often presented as the answer to array insertion problems. In practice, arrays win more often than that suggests.
Cache misses are expensive. Arrays store elements side by side, so accessing one brings its neighbors into the CPU cache for free. Linked list nodes can be anywhere in RAM. Each current = current->next is potentially a fresh cache miss, forcing the CPU to wait for data to arrive from main memory. On modern hardware, cache misses can be 100x slower than cache hits.
Memory overhead adds up. Every node carries at least one pointer (8 bytes on 64-bit systems). A list of integers uses more memory for pointers than for the actual data.
No random access. Algorithms that need to jump to arbitrary positions (binary search, sorting by index, anything that says list[i]) don't work directly on linked lists.
Arrays often win the middle insert too. For many "insert in the middle" workloads, a dynamic array (std::vector in C++) outperforms a linked list in practice, because the data stays cache-friendly. The O(n) shift is often faster than O(n) pointer chasing through scattered memory.
Linked lists fit best when you need O(1) insertions and deletions at the head, when you're implementing a higher-level abstraction (stack, queue, LRU cache), or when memory reallocation is expensive in the environment you work in.
Summary
Linked lists are the first pointer-based data structure. They give up the array's direct addressing for a structure that grows and changes without moving data:
- Insertion and deletion at the head are O(1): nothing shifts, a pointer is redirected
- Traversal is O(n), because the only way to a node is walking the chain
- The size is dynamic, so the list grows and shrinks without reallocation
- Memory is scattered, which is cache-unfriendly: each node is a potential cache miss
- The variants fit different needs: singly (light), doubly (flexible), circular (cyclic)
- Arrays make random access cheap and mutation expensive, and linked lists do the opposite
- A node points to another node of the same type, and trees, graphs and most other pointer-based structures are built on that self-referential pattern