Merge Two Delivery Routes
Easy · Linked Lists · about 15 min
Two couriers each have a route: a linked list of house numbers sorted from smallest to largest. The company wants one combined route, still sorted from smallest to largest.
You get the two routes as plain lists a and b. Build the chains with the given build helper, merge them by relinking the nodes (no sorting), and return the merged chain as a list with to_list.
- Keep duplicates: if both routes visit house 5, it appears twice.
- Either route can be empty.
Example: merge_routes([1, 4, 7], [2, 3, 8]) → [1, 2, 3, 4, 7, 8]
Hints
- A dummy node in front of the result means you never have to treat the first node as a special case.
- While both chains still have nodes, compare their front values and attach the smaller one to the tail.
- When the loop ends, one chain may still have nodes left. Attach the whole leftover chain to the tail.
Learn the pattern first: the Linked Lists lesson.
More Linked Lists problems
- Cancel the K-th Last Booking (Easy)
- Flip a Section of the Parade (Medium)
- Deal From Both Ends (Medium)
- Does the Treasure Hunt Loop? (Medium)
- Add Two Digit Chains (Medium)