Skip to main content

3. Merge Two Sorted Lists

easyAsked at Intuit

Merge two sorted singly-linked lists into one sorted list. Intuit asks this as a linked-list warm-up — useful conceptually for reconciliation merge passes between two sorted ledger streams.

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

Source citations

Public interview reports confirming this problem appears in Intuit loops.

  • Glassdoor (2025-12)QuickBooks online platform team screen included Merge Two Sorted Lists.
  • Blind (2026-Q1)Intuit phone-screen mention; followed by 'how would you reconcile two sorted bank-feed streams?'

Problem

You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.

Constraints

  • The number of nodes in both lists is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.

Examples

Example 1

Input
list1 = [1,2,4], list2 = [1,3,4]
Output
[1,1,2,3,4,4]

Example 2

Input
list1 = [], list2 = []
Output
[]

Example 3

Input
list1 = [], list2 = [0]
Output
[0]

Approaches

1. Convert to arrays, sort, rebuild

Walk both lists collecting values into an array, sort, then build a new linked list.

Time
O((n+m) log (n+m))
Space
O(n+m)
function mergeTwoLists(l1, l2) {
  const arr = [];
  while (l1) { arr.push(l1.val); l1 = l1.next; }
  while (l2) { arr.push(l2.val); l2 = l2.next; }
  arr.sort((a,b) => a-b);
  const dummy = { next: null };
  let cur = dummy;
  for (const v of arr) { cur.next = { val: v, next: null }; cur = cur.next; }
  return dummy.next;
}

Tradeoff: Throws away the sortedness of the inputs. O((n+m) log (n+m)) instead of O(n+m). Mention only as anti-pattern.

2. Two-pointer splice with dummy head (optimal)

Use a dummy node + tail pointer. Walk both lists, attach the smaller head, advance. After the loop attach the remaining tail.

Time
O(n+m)
Space
O(1)
function mergeTwoLists(l1, l2) {
  const dummy = { val: 0, 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: Linear, in-place splice — no node allocation. The dummy head eliminates special-case logic for the first node.

Intuit-specific tips

Intuit interviewers value the dummy-head pattern as a 'clean-code' signal. They also probe whether you'd allocate new nodes or splice the existing ones — splicing is faster and matches how reconciliation engines merge two sorted ledger streams. Bonus signal: discuss stability (when values tie, l1's node comes first — useful when ordering matters for audit logs).

Common mistakes

  • Forgetting to attach the leftover tail (tail.next = l1 || l2) — drops the rest of the longer list.
  • Not using a dummy head — leads to messy if/else for the first node.
  • Modifying l1.val instead of relinking — slow and creates surprising aliasing bugs.

Follow-up questions

An interviewer at Intuit may pivot to one of these next:

  • Merge K Sorted Lists (LC 23) — heap-based extension.
  • Merge two sorted ledger streams where each row has a timestamp + amount.
  • Merge in-place when both lists share nodes from a free list.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why use a dummy head?

It eliminates the first-node special case. Without it you'd need an if/else to set the initial head, which doubles the code length and adds a bug surface.

Is recursion better here?

Recursive is cleaner to read but uses O(n+m) stack space. Iterative is O(1) extra space — preferred at Intuit scale where lists can be long.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →