Data Structures & AlgorithmsVery hard

Floyd's cycle detection finds the slow and fast pointers meeting inside the loop. How is the START of the loop then found?

  1. A. Reverse the list and run the pointers again from the tail.
  2. B. Restart the slow pointer at the head and advance both pointers one step at a time; they meet at the loop start.
  3. C. Count the nodes until the first repeated value appears.
  4. D. Advance only the fast pointer until it returns to the meeting point.

Correct answer: B. Restart the slow pointer at the head and advance both pointers one step at a time; they meet at the loop start.

The distance from the head to the loop start equals the distance from the meeting point to the loop start (modulo the loop length). Moving one pointer back to the head and stepping both at speed one makes them meet exactly at the start.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic