Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

Two sorted linked lists [7 → 13 → 23 → 40] and [5 → 6 → 9 → 18 → 25] are merged with the standard O(n+m) two-pointer merge (always take the smaller head, splicing nodes, no new array). What is the merged list?

  1. A. 7 → 13 → 23 → 40 → 5 → 6 → 9 → 18 → 25
  2. B. 5 → 6 → 7 → 9 → 13 → 18 → 23 → 25 → 40
  3. C. 7 → 5 → 13 → 6 → 23 → 9 → 40 → 18 → 25
  4. D. 40 → 25 → 23 → 18 → 13 → 9 → 7 → 6 → 5

Correct answer: B. 5 → 6 → 7 → 9 → 13 → 18 → 23 → 25 → 40

At each step the merge compares the current heads of both lists and splices the smaller one onto the result, advancing only that list's pointer; this produces the fully sorted list [5 → 6 → 7 → 9 → 13 → 18 → 23 → 25 → 40] in one linear pass. Simply concatenating the two lists, [7 → 13 → 23 → 40 → 5 → 6 → 9 → 18 → 25], is not sorted.

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

Exams that ask this topic