Boss lesson · Unit 03
Reverse and runner
Three pointers to reverse, two speeds to find the middle
What you practise
- Iterative reversal with three pointers
- Slow/fast for the middle and for cycles
Try a question from this lesson
Pick one
You walk a linked list and turn each pointer around so that it faces backwards. What is the danger at every node?
Correct answer
Losing the way forward: the old pointer is overwritten
Why
A node's pointer is the only record of what comes after it. Turn it around without saving it first and the rest of the list is unreachable. No value moves and no node is created.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Three pointers
Reversing in place uses three variables. prev is the head of the part already reversed, and starts empty. node is the node being turned.
Each pass: save node.next, point node back at prev, then move prev and node one place forward. When node runs off the end, prev is the new head.
Concept
The runner: two speeds
Start two pointers at the head. slow moves one node per step; fast moves two.
When fast reaches the end, slow has covered half the distance: it is at the middle. One pass, no counting, no length needed.
10 more exercises in the app
- 1× order the lines
- 2× multiple choice
- 2× fill the blanks
- 4× type the answer
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.