GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A singly linked list has 25 nodes. A routine places pointer p at the head and pointer q at the 6th node from the head, then advances both one step at a time until q->next == NULL. Which node (counted from the head, 1-indexed) does p point to at the end, and how many nodes have been visited by q in all, counting its initial position?

  1. A. node 19, 20 visited
  2. B. node 20, 19 visited
  3. C. node 20, 20 visited
  4. D. node 21, 25 visited

Correct answer: C. node 20, 20 visited

q starts at node 6 and stops at node 25 (q->next == NULL): 19 advances, so p reaches node 1 + 19 = 20. q visits nodes 6 to 25 = 20 nodes counting its start.

More Programming and Data Structures (GATE) questions

Exams that ask this topic