3. Merge Two Sorted Lists
easyAsked at SoFiMerge two sorted linked lists — SoFi maps this to merging two sorted streams of loan-payment events.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
You are given the heads of two sorted singly linked lists. Return a single sorted list spliced from the two inputs.
Constraints
0 <= n1, n2 <= 50-100 <= Node.val <= 100Both lists are sorted ascending
Examples
Example 1
l1 = [1,2,4], l2 = [1,3,4][1,1,2,3,4,4]Example 2
l1 = [], l2 = [][]Approaches
1. Brute force
Collect all values, sort, build a new list.
- Time
- O((n+m) log(n+m))
- Space
- O(n+m)
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);
// build linked list from arrTradeoff:
2. Two-pointer merge
Walk both lists, splicing the smaller node onto a dummy tail. Linear time and the cleaner answer SoFi expects.
- Time
- O(n+m)
- Space
- O(1)
function mergeTwoLists(l1, l2) {
const dummy = { 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:
SoFi-specific tips
SoFi expects clean dummy-head technique and a verbal note that the two-pointer merge mirrors how their payment-reconciliation service joins ACH and card streams.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Merge Two Sorted Lists and other SoFi interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →