Skip to main content

21. Merge Two Sorted Lists

easyAsked at HubSpot

HubSpot asks Merge Two Sorted Lists to evaluate pointer hygiene and recursive vs. iterative trade-offs — skills that translate directly to merging ordered event streams and sorted activity logs in their CRM timeline features.

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

Source citations

Public interview reports confirming this problem appears in HubSpot loops.

  • Glassdoor (2026-Q1)HubSpot SWE phone-screen reports consistently include linked-list merge as an early-round problem.
  • Blind (2025-10)Listed in HubSpot engineering interview threads as a foundational linked-list problem.

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]

Explanation: Interleave nodes by comparing values: 1,1,2,3,4,4.

Example 2

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

Explanation: An empty list merged with [0] simply returns [0].

Approaches

1. Iterative with dummy head

Create a dummy node to anchor the result. Walk both lists simultaneously, appending the smaller node each time. Attach the remaining non-null list at the end.

Time
O(m + n)
Space
O(1)
function mergeTwoLists(list1, list2) {
  const dummy = { val: -1, next: null };
  let curr = dummy;
  while (list1 !== null && list2 !== null) {
    if (list1.val <= list2.val) {
      curr.next = list1;
      list1 = list1.next;
    } else {
      curr.next = list2;
      list2 = list2.next;
    }
    curr = curr.next;
  }
  curr.next = list1 !== null ? list1 : list2; // attach remainder
  return dummy.next;
}

Tradeoff: O(1) extra space (just the dummy node), O(m+n) time. The dummy head eliminates special-casing the first node. This is the expected production-quality answer at HubSpot.

2. Recursive

At each call, pick the smaller head and recursively merge the rest. Base case: if either list is null, return the other.

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

Tradeoff: Elegant and easy to read, but uses O(m+n) call-stack space. Good to show as an alternative, but the iterative version is preferred for large lists. Mention the stack overflow risk for lists with 50,000+ nodes.

HubSpot-specific tips

Before coding, describe the dummy node pattern: 'I'll use a dummy head to avoid special-casing the first node — a standard linked-list technique.' HubSpot interviewers appreciate when you proactively handle both-null and one-null inputs. They may extend the problem to Merge K Sorted Lists — framing your solution as a 2-way merge that generalizes naturally scores extra points.

Common mistakes

  • Not attaching the remaining non-null list at the end — iterating until both are null is O(m+n) extra iterations and misses stability.
  • Forgetting to advance curr after each attachment — creates an infinite loop at the first node.
  • Returning dummy instead of dummy.next — dummy is the sentinel, not part of the result.
  • Using <= instead of < for the comparison — both are valid for stability; just be consistent and explain your choice.

Follow-up questions

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

  • Merge K Sorted Lists (LC 23) — extend to k lists using a min-heap or divide-and-conquer.
  • Sort List (LC 148) — sort a linked list in O(n log n) using merge sort.
  • How would you merge two sorted doubly-linked lists? (Same algorithm, also update prev pointers.)

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 a dummy node?

It lets you treat the first node identically to all subsequent nodes — no special case for 'the result is currently empty'. Return dummy.next at the end.

Does curr.next = list1 || list2 work in JavaScript?

Not reliably — || uses falsiness, and a node with val=0 is falsy in some patterns. Use an explicit ternary: list1 !== null ? list1 : list2.

Is the merge stable?

Yes, because we use <= — equal elements from list1 come before equal elements from list2. If stability doesn't matter, < also works.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →