Merge Two Sorted Lists

Linked Lists, problem 2 of 8

Merge Two Sorted Lists

Easy

LC #21

mergedummy head

Not attempted yet

You're given the heads of two linked lists, list1 and list2, each sorted in non-decreasing order. Splice their nodes together into one sorted list and return its head.

Example 1

Input: list1 = [1,4,9], list2 = [2,3,10]
Output: [1,2,3,4,9,10]

Example 2

Input: list1 = [], list2 = [0,5]
Output: [0,5]

Example 3

Input: list1 = [], list2 = []
Output: []

Constraints

  • 0 ≤ nodes in each list ≤ 2 · 10⁴
  • -100 ≤ Node.val ≤ 10⁵
  • Both lists are sorted in non-decreasing order

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.