Module 7 · Linked Lists
Practice
Practice~1 min
How to practice this module
Linked-list problems reward pointer discipline: draw the list, name the pointers, then touch links in the right order. Reversal and middle build the surgery habits; cycle and merge lean on fast/slow and dummy nodes; remove-nth needs the gap walked once. Done when all five show Solved in the hub.
Problems
Linked Lists — work them in order; difficulty ascends.
0/5
solved
- 1Reverse Linked ListEasyIterative relinkingWatch for: Save the next pointer before rewiring — three pointers (prev / curr / next) are the whole trick
- 2Middle of the Linked ListEasyFast / slow pointersWatch for: Stop when fast reaches the end; an even-length list yields the second middle
- 3Linked List CycleEasyFast / slow detectionWatch for: Slow moves one, fast moves two; null-check fast and fast.next before advancing
- 4Merge Two Sorted ListsEasyDummy-head mergeWatch for: A dummy head removes the first-node special case; drain whichever list is left, exactly once
- 5Remove Nth Node From EndMediumGap pointerWatch for: Advance the first pointer by n before moving both — when n equals the length you remove the head
Cheatsheet
Linked Lists — Pointer surgery — reverse, meet, and splice without losing the rest.
Smell → pattern
- Reverse order of nodesIterative prev/curr/next
- Middle or cycle?Fast & slow
- Remove nth from endLeader gap of n
- Merge two sorted listsDummy head walk
- Reorder / rotate nodesCut + relink
Patterns
Iterative reverse
CoreSmell: Flip every next without recursion
Three pointers: prev, curr, next. Save next, rewire curr.next → prev, then advance. Return prev — that is the new head.
Fast & slow
SafeSmell: Middle node or cycle detection
Slow +1, fast +2. Middle when fast hits null; cycle when fast meets slow again. Draw one lap — the rhythm sticks.
Dummy head
ReachSmell: Delete or merge that might touch the real head
Fake node before the list so every splice shares one path. Return dummy.next. Eliminates special-case head deletes.
Leader gap
CarefulSmell: Nth from end without knowing length first
Advance a leader n steps (or n+1 if you need the node before the cut). Move both until leader is null — follower sits at the edit point.
Cut and relink
CoreSmell: Rotate, split, or weave two chains
Find the cut with a length or two-pointer pass, then rewire heads/tails. Never lose the second half’s head before you attach it.
Complexity targets
Reverse / middle / merge
- Time
- O(n)
- Space
- O(1)
- Note
- Prefer iterative
Recursive reverse
- Time
- O(n)
- Space
- O(n)
- Note
- Call stack depth
Cycle detect (Floyd)
- Time
- O(n)
- Space
- O(1)
- Note
- No visited set needed
| Move | Time | Space | Note |
|---|---|---|---|
| Reverse / middle / merge | O(n) | O(1) | Prefer iterative |
| Recursive reverse | O(n) | O(n) | Call stack depth |
| Cycle detect (Floyd) | O(n) | O(1) | No visited set needed |
Traps
Losing the next pointer
Save curr.next before rewiring. One missed save and the rest of the list is unreachable — silent data loss, not a crash.
Off-by-one on the gap
Removing the nth-from-end needs the node before it. A gap of n vs n+1 is the usual bug; empty-list and n = length are the stress cases.