merge 2 linkedlist leetcode

#613
Raw
Author
Anonymous
Created
Dec. 14, 2022, 4:34 p.m.
Expires
Never
Size
692 bytes
Hits
50
Syntax
Python
Private
No
#You are given the heads of two sorted linked lists list1 and list2.

#Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.

def mergeTwoLists(self, list1: ListNode, list2: ListNode) -> Optional[ListNode]:
        curr = ret = ListNode()
        while list1 and list2:
            if list1.val < list2.val:
                curr.next = list1
                list1, curr = list1.next, list1
            else:
               curr.next = list2
               list2, curr = list2.next, list2

        if list1 or list2:
            curr.next = list1 if list1 else list2
        
        return ret.next