GATE Computer ScienceVery hardGATE CS (PYQ pattern)
Two singly linked lists merge into a common tail: list X has 7 nodes before the merge point, list Y has 11 nodes before it, and the shared tail has 5 nodes. Pointers p = headX and q = headY advance one node per step; when a pointer is at the last node of its list its next move goes directly to the head of the other list (there is no NULL step), and the process stops when p == q. After how many steps (0 steps = the initial comparison) does the loop stop, and at which shared-tail node (1 = merge point)?
- A. 22 steps, shared node 1
- B. 23 steps, shared node 1
- C. 23 steps, shared node 2
- D. 27 steps, shared node 1
Correct answer: B. 23 steps, shared node 1
p walks list X (12 nodes), then the 11 unique nodes of Y; q walks list Y (16 nodes), then the 7 unique nodes of X. Both have taken 12 + 11 = 16 + 7 = 23 steps when they stand on the merge point.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A singly linked list 1 -> 2 -> ... -> 12 is modified by reversing the sublist from position 3 to position 9 (1-indexed, inclusive), and…
- Eight sorted lists of 100 keys each (800 distinct keys) are merged with the standard two-way merge, whose worst case for lists of sizes p…
- The number of nodes of a complete binary tree is found recursively: compute the leftmost-path height and the rightmost-path height of the…
- A BST is built from the sorted array 1, 2, ..., 14 by recursion: the root is a[mid] with mid = (lo + hi) / 2 (integer division), then the…
- In a binary tree given as (key, left, right) = (10, (-20, (8), (-6, (4), -)), (15, (-3), (12, (7), (-9)))), a 'path' is any sequence of…
- An AVL tree is built by inserting 1, 2, ..., 15 in increasing order (the perfect tree with root 8). Then keys 1, 3, 5, 2, 4 are deleted in…