Skip to content
Decoder

Merge Two Sorted Lists

Easy

Problem

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.

Example 1

Input
a = 1
Output
2 -> 4, b = 1 -> 3 -> 4 → 1 -> 1 -> 2 -> 3 -> 4 -> 4

Example 2

Input
a = (empty), b = 0
Output
0

Example 3

Input
a = (empty), b = (empty)
Output
(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.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.