3. Merge Two Sorted Lists
easyAsked at DropboxMerge two sorted linked lists into one sorted list; Dropbox uses it as a stand-in for merging two sorted delta streams from different clients.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
You are given the heads of two sorted linked lists. Merge them into a single sorted linked list by splicing nodes (not copying values). Return the head.
Constraints
0 <= n <= 50 nodes per list-100 <= Node.val <= 100Both inputs are non-decreasing
Examples
Example 1
l1=[1,2,4], l2=[1,3,4][1,1,2,3,4,4]Example 2
l1=[], l2=[0][0]Approaches
1. Brute force
Collect both into an array, sort, rebuild.
- 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}
return arr.sort((a,b)=>a-b).reduce(...);Tradeoff:
2. Two-pointer splice
Use a dummy head and advance the pointer with the smaller value at each step. No extra allocation.
- Time
- O(n + m)
- Space
- O(1)
function merge(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:
Dropbox-specific tips
Dropbox favors in-place splicing — they map allocation cost to memory pressure on the sync daemon and dock candidates who copy nodes.
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 Dropbox interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →