A linked list stores a sequence as a chain of nodes, where each node holds a value and a reference to the next node (and, in a doubly linked list, to the previous one). Unlike an array, the elements are not contiguous in memory, so you cannot jump to index k in constant time. That single difference drives every trade-off below.
Why Linked Lists Matter#
Linked lists show up in interviews less for their raw usefulness and more because they force you to reason about pointers carefully. Reversing a list, detecting a cycle, or merging two sorted lists all reward candidates who can track “what points to what” without a diagram. They also underpin real structures: hash-table buckets, LRU caches, and adjacency lists all lean on linked-list mechanics.
Key Operations and Their Big-O#
| Operation | Time | Notes |
|---|---|---|
| Access by index | O(n) | No random access; walk from the head |
| Search for a value | O(n) | Linear scan |
| Insert/delete at head | O(1) | The core advantage over arrays |
| Insert/delete at tail | O(1) with tail pointer, else O(n) | Keep a tail reference for queues |
| Insert/delete given the node | O(1) singly (with next), O(1) doubly | Splicing pointers |
The takeaway: linked lists win when you insert and delete near a known node and lose whenever you need indexed access.
A Short Example#
Reversing a singly linked list in place is the canonical warm-up:
1class Node:
2 def __init__(self, val, nxt=None):
3 self.val = val
4 self.next = nxt
5
6def reverse(head):
7 prev = None
8 while head:
9 nxt = head.next # save before we overwrite it
10 head.next = prev # flip the pointer
11 prev = head
12 head = nxt
13 return prev # new head
Common Pitfalls#
- Losing the rest of the list. Overwrite
head.nextbefore savingnxtand everything after is gone. Save first, then reassign. - Forgetting the dummy head. Insertions and deletions at the front become uniform if you start with a sentinel
dummynode and returndummy.next. - Null-pointer edge cases. Empty lists and single-node lists break naive loops. Test both.
- Fast/slow pointer bugs. For cycle detection and finding the middle, advance the fast pointer by two only after confirming
fastandfast.nextare non-null.
Where the Curriculum Covers This#
The 60-day challenge devotes several days to linked lists:
- Day 7: Introduction to Linked Lists
- Day 8: Singly Linked Lists
- Day 9: Doubly Linked Lists
- Day 10: Advanced Linked List Operations
Related Resources#
- Stacks and queues study guide: both are often built on linked lists.
- Big-O cheat sheet: the complexity tables side by side.
- Interview patterns: where the two-pointer tricks generalize.
- Interview prep hub: how linked-list questions fit a full loop.