Merge Two Sorted Lists
Problem statement
Merge two nondecreasing singly linked lists list1 and list2 into one nondecreasing list and return its head.
Function
mergeTwoLists(list1: ListNode, list2: ListNode) → ListNodeExamples
Example 1
list1 = [1,2,4]list2 = [1,3,4]return = [1,1,2,3,4,4]Repeatedly choose the smaller front node.
Example 2
list1 = []list2 = [0]return = [0]An empty list contributes no nodes.
Constraints
- Each list has at most
10^5nodes. - Node values are 32-bit signed integers.