ProgrammingVery hard

In a list 1 -> 2 -> 3 -> 4 -> 2 where the last node points back to the node holding 2, Floyd's method runs slow = slow->next and fast = fast->next->next from the head, and stops when they meet. How many iterations run?

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

Correct answer: B. 3

After iteration 1 slow is at 2 and fast at 3. After iteration 2 slow is at 3 and fast at 2. After iteration 3 both are at 4, so they meet after 3 steps.

More C Programming questions

Exams that ask this topic