Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

A linked list has a tail of 3 nodes leading into a cycle of 5 nodes. A slow pointer advances 1 node per step and a fast pointer advances 2 nodes per step, both starting at the head. After how many steps do they first meet?

  1. A. 4
  2. B. 6
  3. C. 5
  4. D. 10

Correct answer: C. 5

The slow pointer enters the cycle at step 3; from then the fast pointer gains one node per step, so they meet at the first step s >= 3 that is a multiple of the cycle length 5.

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

Exams that ask this topic