Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

Two sorted linked lists A = [1, 4, 6, 9] and B = [2, 3, 7, 10, 12] are merged with the standard two-pointer merge, which compares the current heads once per output node until one list is empty. How many comparisons are performed?

  1. A. 8
  2. B. 4
  3. C. 20
  4. D. 7

Correct answer: D. 7

Each comparison outputs one node; 1,2,3,4,6,7,9 are output by comparisons, then A is empty, so 7. The worst-case formula n + m − 1 gives 8, min(n, m) = 4 is the best case, and 20 = n·m compares every pair.

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

Exams that ask this topic