GATE Computer ScienceVery hardGATE CS

A singly linked list has a tail of 6 nodes leading into a cycle of 9 nodes. A pointer 'slow' moves 1 node per step and a pointer 'fast' moves 3 nodes per step, both starting at the head. After how many steps do the two pointers first point to the same node?

  1. A. 6
  2. B. 9
  3. C. 12
  4. D. 15

Correct answer: B. 9

Both are in the cycle once slow has taken at least 6 steps. Inside the cycle the gap grows by 2 per step, so they meet when 2t ≡ 0 (mod 9) and t ≥ 6, i.e. when t is a multiple of 9. The first such t is 9.

More Programming and Data Structures (GATE) questions

Exams that ask this topic