Zero To DSAZero To DSA
Privacy Policy
Reverse Linked ListLinked List Cycle

Merge Two Sorted Lists

easy
Time: O(n + m)
Space: O(1)

Merge two sorted linked lists and return it as a sorted new list.

Constraints

  • The number of nodes is in the range [0, 50].

Examples

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