Middle of the Parade
A town parade is a line of floats. Each driver only knows which float is right behind them. Nobody has the full list or knows how long the parade is.
The TV crew wants to film from the middle float. Starting at the lead float and following "who is behind you", find it.
Your task
Write middleFloat(head). head is the first node of a linked list (each node has val and next). Return the middle node.
With an odd number of floats there is one middle. With an even number there are two; return the second. The tests read the list from the node you return to the end, so the middle of [1, 2, 3, 4, 5] shows as [3, 4, 5].
middleFloat(head: ListNode) → ListNode
Examples
head = [8, 3, 6, 2, 9]
[6, 2, 9]
Five floats. The third, 6, has two ahead and two behind. From 6 the parade reads 6, 2, 9.
head = [8, 3, 6, 2]
[6, 2]
Four floats, so 3 and 6 are both in the middle. The rule picks the second, 6. Returning 3 would show [3, 6, 2] and fail.
Constraints
- 1 ≤ number of floats ≤ 10,000
- Float ids are integers and may repeat.
- Return a node from the list you were given; do not build a new list.
Start with brute force
Walk the parade once to count the floats, n. Walk again from the start and stop after n / 2 steps (rounded down).
That is O(n) and correct, but takes two passes. The trick here finds the middle in one pass without knowing n, which you need when the list is read once, like a stream, or inside a bigger list algorithm.
Hints
0 of 4 shown- Hint 1
Two friends start at the lead float. One walks one float at a time, the other two at a time. Where is the slow walker when the fast walker reaches the end of the parade?
- Hint 2
The fast walker covers twice the slow one's distance. So when the fast one reaches the end, the slow one is halfway.
- Hint 3
Check the stopping rule with 2 floats and with 4 floats, and confirm that the slow walker stops on the second middle.
- Hint 4
Start
slowandfastathead. Whilefastandfast.nextboth exist, moveslowone step andfasttwo. Returnslow.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
You do not need the length. Send two pointers down the list at different speeds; when the fast one runs out, the slow one is halfway.
Trace 8 → 3 → 6 → 2 → 9:
- Both start at 8. Since fast has a next node, move slow to 3 and fast to 6.
- Fast is at 6 and has a next node, so move slow to 6 and fast to 9.
- Fast is at 9 and has no next node, so the loop stops and the function returns 6.
With 8 → 3 → 6 → 2: after one move slow = 3, fast = 6; after the second slow = 6, fast = null. The loop stops and slow sits on the second middle, 6. For the first middle, stop one step earlier by checking fast.next.next.
function middleFloat(head) {
let slow = head;
let fast = head;
// fast moves 2 steps for every 1 step of slow
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}def middle_float(head):
slow = fast = head
# fast moves 2 steps for every 1 step of slow
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
return slowRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.