Data Structures & AlgorithmsModerateWipro NLTH · Linked List

A singly linked list has 10 nodes numbered 1 to 10. Slow and fast pointers both start at node 1; the loop is `while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; }`. Which node does slow end on?

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

Correct answer: D. 6

Fast moves 2 nodes per step and runs off the list after 5 iterations, by which time slow has moved 5 nodes, to node 6 (the second middle).

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

Exams that ask this topic