Gamified challenge · Fast and slow pointers
A track is a singly linked list of stations (ListNodes, numbered by position from 0 at the head). The last station either ends the track (next is null) or links back to an earlier station, making a loop you could ride forever.
Write loopStart(track) (Python: loop_start(track)) that returns the index of the station where the loop begins, or -1 if the track has no loop. Station values may repeat, so compare nodes, not values. Try it with O(1) extra memory.
For 3 → 2 → 0 → -4 with the last station linking back to the 2, the answer is 1. A track with no stations returns -1.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google