Fast and Slow Pointers

Fast and slow pointers: two pointers moving at different speeds that find a list's middle, detect a cycle and locate where the cycle starts.

  • 8 cards
  • 4 questions
  • About 23 min
  • Intermediate to Advanced
Share

Fast and slow pointers walk one sequence at two speeds, one node a move and two nodes a move, and read an answer from where they are when the fast one stops or catches up. It matters because a linked list has no index and often no known length, and these two pointers find its middle, prove whether it loops and find where the loop begins in O(1) extra memory. The topic starts with the middle of a list, derives the cycle check from a set of visited stops on a delivery route, and gives the two-speed loop in three languages. It finds where a cycle starts with a second walk, applies both phases to codes computed from a rule with no list at all, shows why the meeting point is not the start, and ends with an exercise on a looping treasure trail.

Contents

8 cardsnot started