Notice Period

Merge Two Sorted Lists

easy

Two warehouse conveyor lines each deliver packages already sorted by weight. You're building the merge station: splice both lines into a single line, still sorted, reusing the existing nodes — no new packages, just rerouted track.

Given the heads of two sorted linked lists, merge them into one sorted list and return its head. Tests pass plain arrays; implement the core in _merge on real nodes.

Example 1
in a = [1, 2, 4], b = [1, 3, 4]
out [1, 1, 2, 3, 4, 4]
Example 2
in a = [], b = []
out []
Example 3
in a = [], b = [0]
out [0]
Merging with an empty list returns the other list.
Constraints
  • · 0 <= nodes in each list <= 50
  • · -100 <= node value <= 100
  • · both inputs sorted ascending
  • · reuse nodes — no new allocations for data
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.