Skip to main content

3. Merge Two Sorted Lists

easyAsked at Salesforce

Merge two sorted linked lists into one sorted list. Salesforce uses this to test whether you handle pointer arithmetic cleanly and reach for a dummy-head sentinel to simplify edge cases.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Backend engineer phone screen for Service Cloud team.
  • LeetCode Discuss (2025)Frequently combined with k-way merge variants for scheduling problems.

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. Collect values, sort, rebuild

Walk both lists, push values into an array, sort, build a new list.

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

Tradeoff: Throws away the fact that inputs are already sorted. Salesforce will dock you for this.

2. Dummy-head two-pointer merge

Use a dummy node so you never special-case the head. Walk both lists, append the smaller node, advance.

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

Tradeoff: Linear time, constant space, and the dummy-head trick eliminates the 'is this the first node?' branch entirely.

Salesforce-specific tips

Salesforce values clean pointer code — they grade on whether you reach for the dummy-head sentinel without prompting. Bonus signal: mention that this is the building block of k-way merge, which Salesforce uses heavily in their report aggregation pipeline and bulk-API result merging.

Common mistakes

  • Forgetting cur.next = l1 || l2 to attach the leftover tail — drops the longer list's remainder.
  • Creating new nodes instead of splicing existing ones — wastes memory and breaks the in-place expectation.
  • Returning dummy instead of dummy.next — gives a leading 0 in the output.

Follow-up questions

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

  • Merge k sorted lists (LC 23).
  • Merge two sorted lists in-place without dummy node — how does the code change?
  • What if the lists are doubly-linked?

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 an if/else for the first node (since no prior node exists to attach to). The dummy lets you treat every iteration uniformly.

Is the iterative or recursive solution preferred?

At Salesforce, iterative is preferred because the recursive version blows the stack on lists of 10K+ nodes — and they care about production-grade code.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →