Linked List Basics
Read a little, play a little. No scary maths, and no rush.
A chain of sticky notes
An array is a row of lockers — everything numbered, side by side. A linked list is a scavenger hunt: each sticky note holds a value and the location of the next note. To find the 5th item, you walk notes 1 through 4. Slow to search, but inserting a note in the middle costs nothing — just rewrite two arrows instead of shifting a thousand lockers.
class N:
def __init__(self, v, n=None):
self.v = v # the value
self.next = n # where the next note lives
The one move to master: reversing
Reversing a linked list is the "hello world" of pointer problems, and it teaches the only skill that matters here — rewiring arrows without losing any:
def reverse(h):
prev = None
while h:
nxt = h.next # 1. save the rest of the chain
h.next = prev # 2. flip this arrow backwards
prev = h # 3. step forward
h = nxt
return prev
Walk it with 1 → 2 → 3: after one pass, 1 points at nothing, prev is 1, h is 2. Repeat until h falls off the end. Forgetting step 1 orphans the rest of the list — the classic bug, so say it aloud in interviews.
Two runners, one track
Cycle detection uses the fast and slow pointer trick: send one runner at 1 step and another at 2 steps. On a straight track the fast one finishes; in a loop, the fast one laps around and must eventually land on the slow one — like two runners on a circular track always meeting. Same trick finds the middle of a list and the kth node from the end.
Everyday patterns
- Dummy head: a fake first node so deleting the real head needs no special case.
- Merge two sorted lists: compare heads, attach the smaller, advance — like zipping two zippers.
- If you need random access by index, you picked the wrong structure — use an array.
Merging two sorted lists, traced
Merge 1 → 3 → 5 with 2 → 4. Start from a dummy node and keep a tail pointer:
- Compare 1 and 2: attach 1, advance the first list.
- Compare 3 and 2: attach 2, advance the second.
- Compare 3 and 4: attach 3. Compare 5 and 4: attach 4.
- The second list is empty, so attach the rest of the first list, 5, in one step.
Result: 1 → 2 → 3 → 4 → 5, returned as dummy.next. The dummy node saved a special case for "which node is the head?", the same trick as in deletion.
Arrays or linked lists in real code?
In practice arrays are faster for most jobs, even some that look like linked-list territory, because their items sit next to each other in memory and the processor reads them very efficiently. Linked lists earn their place inside other structures: an LRU cache pairs a hash map (find any entry in O(1)) with a doubly linked list (move an entry to the front, or evict the oldest, in O(1)). That combination is a common interview question and shows exactly what linked lists are good at.
Remember this
- Draw boxes and arrows before writing code; pointer bugs are drawing bugs.
- Reverse iteratively and Floyd's cycle check cover most linked-list interviews.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Related chapters
Spotted a mistake or want a topic covered? Report an issue