3. Merge Two Sorted Lists
easyAsked at BookingMerge two sorted linked lists — Booking uses this to test pointer manipulation that mirrors merging two supplier availability streams.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Merge two sorted linked lists and return it as a sorted list. The new list is made by splicing together the nodes of the first two lists.
Constraints
0 <= each list length <= 50-100 <= Node.val <= 100Both lists are sorted in non-decreasing order
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 array sort
Dump both lists 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;}
arr.sort((a,b)=>a-b);Tradeoff:
2. Two-pointer splice
Walk both lists with a dummy head; attach the smaller node each step.
- 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:
Booking-specific tips
Booking will probe whether you can extend this to merging hotel availability feeds from multiple suppliers — mention k-way merge as a follow-up.
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 Booking interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →