diff --git a/merge-two-sorted-lists/yayyz.py b/merge-two-sorted-lists/yayyz.py new file mode 100644 index 000000000..bdb9b01ef --- /dev/null +++ b/merge-two-sorted-lists/yayyz.py @@ -0,0 +1,25 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(0) + current = dummy + + while list1 is not None and list2 is not None: + if list1.val < list2.val: + current.next = list1 + list1 = list1.next + else: + current.next = list2 + list2 = list2.next + current = current.next + + if list1 is not None: + current.next = list1 + else: + current.next = list2 + + return dummy.next