Skip to main content

21. Merge Two Sorted Lists

easyAsked at JPMorgan

Merge two sorted linked lists into one sorted list. JPMorgan asks this on Software Engineer Programme phone screens because it tests pointer manipulation cleanly and sets up natural follow-ups (merge k lists, merge sorted arrays).

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

Source citations

Public interview reports confirming this problem appears in JPMorgan loops.

  • Glassdoor (2026-Q1)JPMorgan SDE phone-screen reports list this as the standard linked-list warm-up.
  • LeetCode (2026-Q1)Tagged JPMorgan on the LeetCode company tag page.
  • Blind (2025-11)JPMC SWE-I write-up: 'merge two lists then asked for the k-list extension'.

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. Iterative splice with sentinel head (optimal)

Allocate a sentinel node. Walk both lists with a tail pointer; on each step, splice the smaller node onto tail. Append the remainder when one list runs out.

Time
O(n + m)
Space
O(1)
function mergeTwoLists(list1, list2) {
  const sentinel = { val: 0, next: null };
  let tail = sentinel;
  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 sentinel.next;
}

Tradeoff: O(n + m) time and O(1) extra space — the sentinel removes the head edge-case branch. This is the answer most JPMorgan interviewers expect; mention the sentinel by name.

2. Recursive

Pick the smaller head; recursively merge the rest.

Time
O(n + m)
Space
O(n + m) call stack
function mergeTwoListsRec(list1, list2) {
  if (list1 === null) return list2;
  if (list2 === null) return list1;
  if (list1.val <= list2.val) {
    list1.next = mergeTwoListsRec(list1.next, list2);
    return list1;
  }
  list2.next = mergeTwoListsRec(list1, list2.next);
  return list2;
}

Tradeoff: Concise and clean but consumes O(n + m) stack — fine for the n=50 constraint, risky on the k-list follow-up where lists can be long. Mention both versions if you have time.

JPMorgan-specific tips

JPMorgan interviewers explicitly grade the sentinel-node usage. The most common bug they see is candidates trying to handle the 'which list owns the head' branch in code instead of with a sentinel. Articulate the sentinel choice up front: 'I will allocate a dummy node so I do not have to special-case the first append.'

Common mistakes

  • Forgetting to attach the remainder of the longer list after the shorter runs out — produces a truncated result.
  • Allocating a brand-new node for every comparison instead of splicing existing nodes — violates the 'splicing together the nodes' instruction.
  • Using strict < instead of <= on the comparison — works on most inputs but loses stability when both heads are equal.
  • Forgetting to advance tail after splicing — the resulting list has tail repeatedly overwritten.

Follow-up questions

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

  • Merge k sorted lists (LC 23) — min-heap or pairwise merge.
  • Merge two sorted arrays in-place (LC 88) — walk backward.
  • What if the lists are doubly linked?
  • What if lists are sorted in descending order — what changes?

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 the sentinel head?

Without one, the very first append needs a special case ('is the result list empty yet?') and an explicit head variable. The sentinel collapses that branch into the same code path as every other append, halving the bug surface.

Is the recursive solution ever a bad choice?

On this specific problem with n=50 it is fine. On the natural follow-up (merge k sorted lists where each can be 10000 long), the recursion depth blows the stack in many environments — the iterative version generalises better.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →