Skip to main content

3. Merge Two Sorted Lists

easyAsked at Zoom

Merge two sorted linked lists into one sorted list.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

You are given the heads of two sorted linked lists list1 and list2. Splice the nodes together into one sorted list and return its head.

Constraints

  • 0 <= count <= 50 per list
  • -100 <= Node.val <= 100
  • Both lists are sorted non-decreasing

Examples

Example 1

Input
l1=[1,2,4], l2=[1,3,4]
Output
[1,1,2,3,4,4]

Example 2

Input
l1=[], l2=[0]
Output
[0]

Approaches

1. Copy to array and sort

Collect values, sort, rebuild list.

Time
O((n+m) log(n+m))
Space
O(n+m)
const v=[]; while(l1){v.push(l1.val);l1=l1.next} while(l2){v.push(l2.val);l2=l2.next}
v.sort((a,b)=>a-b); return buildList(v);

Tradeoff:

2. Two-pointer splice

Walk both heads with a tail pointer and stitch the smaller node next. No extra allocation.

Time
O(n+m)
Space
O(1)
function merge(l1, l2) {
  const dummy = { next: null };
  let tail = dummy;
  while (l1 && l2) {
    if (l1.val <= l2.val) { tail.next = l1; l1 = l1.next; }
    else { tail.next = l2; l2 = l2.next; }
    tail = tail.next;
  }
  tail.next = l1 || l2;
  return dummy.next;
}

Tradeoff:

Zoom-specific tips

Zoom often uses merged-list problems as a warmup before merging time-ordered participant event streams (joins, leaves, mute toggles) — call out that connection.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Merge Two Sorted Lists and other Zoom interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →