One Van, Two Routes
Two couriers deliver along the same long street. Each route is a chain of stops, each pointing to the next, sorted by distance from the depot in metres.
One courier calls in sick, so one van must do both rounds in a single drive. The dispatcher needs one chain with every stop from both routes, still sorted by distance.
Your task
Write mergeRoutes(first, second). Both arguments are sorted linked lists (smallest distance first; null / None means no stops). Return the head of one sorted list that contains every node of both.
If both routes have a stop at the same distance, keep both stops. Reuse the existing nodes by relinking them rather than creating new ones.
mergeRoutes(first: ListNode, second: ListNode) → ListNode
Examples
first = [2, 5, 9] second = [3, 5, 12]
[2, 3, 5, 5, 9, 12]
The merge takes 2 first because it is smaller than 3. It then takes 3, which is smaller than 5, then both 5s, then 9, which is smaller than 12, and finally the leftover 12.
first = [] second = [7, 8]
[7, 8]
The first courier had no stops, so the van follows the second route.
Constraints
- 0 ≤ stops in each route ≤ 5,000
- -100,000 ≤ distance ≤ 100,000 (negative means west of the depot)
- Each route is sorted in non-decreasing order.
Start with brute force
Copy every distance into an array, sort it, and build a new chain.
The sort costs O((n + m) log(n + m)), ignores that both routes were already sorted, and allocates a new chain. Merging walks each route once, O(n + m), and reuses the nodes.
Hints
0 of 4 shown- Hint 1
Look only at the first stop of each route. Which must be the first stop of the merged route?
- Hint 2
The smaller front stop goes first. Take it off its route, and you have the same question with one stop fewer.
- Hint 3
Keep a
tailpointer to the last merged stop so you can attach the next one in O(1). A dummy starting node means the first attachment is not a special case. - Hint 4
Create a dummy node and set
tail = dummy. While both routes have stops, attach the smaller front node totail.next, advance that route and advancetail. When one route runs out, attach the rest of the other in one step. Returndummy.next.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The merged route starts with the smaller of the two front stops, and the same holds for what is left. So repeatedly compare the two fronts and move the smaller to the end of the result.
Trace 2 → 5 → 9 and 3 → 5 → 12:
- The fronts are 2 and 3, so attach 2. The result is dummy → 2.
- The fronts are 5 and 3, so attach 3. The result is … → 2 → 3.
- The fronts are 5 and 5. They are equal, so take the first route's 5. The result is … → 3 → 5.
- The fronts are 9 and 5, so attach 5. The result is … → 5 → 5.
- The fronts are 9 and 12, so attach 9. The first route is now empty.
- Attach the rest of the second route,
12, in one step. Returndummy.next:2 → 3 → 5 → 5 → 9 → 12.
The dummy node removes the "is the result still empty?" check. Without it, the first attachment needs special code, and that is where bugs hide.
function mergeRoutes(first, second) {
const dummy = new ListNode(0, null); // placeholder before the real head
let tail = dummy;
while (first !== null && second !== null) {
if (first.val <= second.val) {
tail.next = first;
first = first.next;
} else {
tail.next = second;
second = second.next;
}
tail = tail.next;
}
tail.next = first !== null ? first : second; // the leftover is already sorted
return dummy.next;
}def merge_routes(first, second):
dummy = ListNode(0) # placeholder before the real head
tail = dummy
while first is not None and second is not None:
if first.val <= second.val:
tail.next = first
first = first.next
else:
tail.next = second
second = second.next
tail = tail.next
tail.next = first if first is not None else second # leftover is sorted
return dummy.nextRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.