diff --git a/LeetCode/easy/merge_two_lists_21.py b/LeetCode/easy/merge_two_lists_21.py new file mode 100644 index 0000000..d20aacd --- /dev/null +++ b/LeetCode/easy/merge_two_lists_21.py @@ -0,0 +1,29 @@ +from typing import Optional + + +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() + tail = dummy + + while list1 and list2: + if list1.val < list2.val: + tail.next = list1 + list1 = list1.next + else: + tail.next = list2 + list2 = list2.next + tail = tail.next + + if list1: + tail.next = list1 + else: + tail.next = list2 + + return dummy.next