Skip to content

Merge Two Sorted Lists

EasyLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

You get the heads of two linked lists, a and b, each already sorted in ascending order. Splice them together into one sorted list and return its head. Reuse the existing nodes rather than making new ones.

a = 1 -> 2 -> 4,  b = 1 -> 3 -> 4   →  1 -> 1 -> 2 -> 3 -> 4 -> 4
a = (empty),      b = 0             →  0
a = (empty),      b = (empty)       →  (empty)

If both are empty, return None (null in TypeScript).

Constraints

  • 0 <= number of nodes in each list <= 50
  • -100 <= Node.val <= 100
  • Both lists are sorted in non-decreasing order.