dsa · easy
Merge Two Sorted Lists
You are given the heads of two sorted singly linked lists. Merge them into one sorted list and return the new head. Empty lists are allowed; merging with empty is the other list.
Arguments
list1— the first sorted linked list as a ListNode (val,next);Noneif emptylist2— the second sorted linked list as a ListNode (val,next);Noneif empty
Walk .next on each ListNode. The judge prints the resulting values.
Example
list1 = [1,2,4], list2 = [1,3,4] → [1,1,2,3,4,4].
Constraints
0 <= lengths <= 50
Examples
Example 1
Input: [1,2,4] [1,3,4] Expected: [1,1,2,3,4,4]
Example 2
Input: [] [] Expected: []