3. Merge Two Sorted Lists
easyAsked at FlipkartMerge two sorted linked lists into one sorted list — Flipkart asks this to test pointer manipulation before scaling to merging sorted shipment manifests.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
You are given the heads of two sorted linked lists list1 and list2. Merge them into a single sorted list by splicing nodes together and return the head.
Constraints
0 <= nodes <= 50-100 <= Node.val <= 100Both lists are sorted non-decreasing
Examples
Example 1
list1 = [1,2,4], list2 = [1,3,4][1,1,2,3,4,4]Example 2
list1 = [], list2 = [0][0]Approaches
1. Collect and sort
Dump both into an array and sort.
- 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); // rebuild listTradeoff:
2. Two-pointer splice
Maintain a dummy head and pick the smaller current 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:
Flipkart-specific tips
Flipkart screens reward candidates who reach for the dummy-head pattern without prompting — it is the same trick they use to merge inventory feeds from multiple warehouses.
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 Flipkart interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →