Skip to content
Onsite

Merge Two Sorted Linked Lists

EasyP1Coding

Given the heads of two sorted singly linked lists list1 and list2, merge them into one sorted list by splicing together their nodes. Return the head of the merged list.

A list node has val and next. The ListNode class is already defined for you.

Example 1

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

Example 2

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

Example 3

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

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 8.