Skip to main content

3. Merge Two Sorted Lists

easyAsked at Figma

Merge two sorted linked lists into one sorted list by splicing pointers. Figma uses this to probe pointer manipulation — the same fundamentals their CRDT operation log needs when merging two streams of edits.

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

Source citations

Public interview reports confirming this problem appears in Figma loops.

  • LeetCode Discuss (2025)Frequently surfaced as a Figma phone-screen pointer problem.
  • Glassdoor (2026-Q1)Figma engineering interview, IC3 level.

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
[]

Approaches

1. Collect-then-sort

Push all values into an array, sort, rebuild a list.

Time
O((m+n) log(m+n))
Space
O(m+n)
function mergeTwoLists(a, b) {
  const vals = [];
  for (let n = a; n; n = n.next) vals.push(n.val);
  for (let n = b; n; n = n.next) vals.push(n.val);
  vals.sort((x,y) => x - y);
  // rebuild...
}

Tradeoff: Throws away the fact that the inputs are already sorted. Fails the spirit of the question.

2. Two-pointer splice with dummy head

Use a dummy head; advance whichever list has the smaller current head, attach it to tail. Append remainder at the end.

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

Tradeoff: Single pass, O(1) extra space. The dummy head removes a special case for the very first append.

Figma-specific tips

Figma watches for the dummy-head pattern as a sign of pointer-fluency, the same fluency their CRDT log needs to merge two sequences of edits without losing any. They like candidates who narrate the invariant 'tail is the last node placed in the merged list' before coding, and who handle the trailing remainder without a second loop.

Common mistakes

  • Forgetting to attach the non-null tail at the end — drops half the data when one list is exhausted.
  • Creating new nodes instead of splicing the existing ones — uses O(n) extra space.
  • Off-by-one when initializing the dummy — write 'tail.next = ...; tail = tail.next;' as a pair.

Follow-up questions

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

  • Merge k sorted lists (LC 23) — heap or divide-and-conquer.
  • Merge two sorted arrays in place (LC 88).
  • Maintain stability when values are equal — does the order of list1 vs list2 matter?

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?

Without it you'd need a branch on the very first iteration ('is the merged list empty yet?'). The dummy lets you treat every step uniformly.

Does this work with one empty list?

Yes. The while loop doesn't execute, and 'tail.next = list1 || list2' attaches whichever is non-null, returning it via dummy.next.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →