Loading practice problems…
Loading your practice workspace…
Loading code editor…
22 / 29
Sign in
Algorithms/Linked List

Merge Two Sorted Lists

easy25 min

Given the heads of two sorted singly linked lists, combine their existing nodes into one sorted list.

Return the head of the merged list. Each node has the shape { val, next }.

Example 1

Inputlist1 = [1, 2, 4], list2 = [1, 3, 4]

Output[1, 1, 2, 3, 4, 4]

Taking the smaller available node from the two lists places both 1s first, then 2, 3, and both 4s.

Example 2

Inputlist1 = [], list2 = []

Output[]

Both lists are empty, so there are no nodes to merge.

Constraints

  • 0 ≤ total number of nodes ≤ 100
  • Both input lists are sorted in non-decreasing order.
Linked ListTwo Pointers