diff --git a/leetCode/LinkedList/merge-two-sorted-lists.py b/leetCode/LinkedList/merge-two-sorted-lists.py new file mode 100644 index 0000000..ceadc1d --- /dev/null +++ b/leetCode/LinkedList/merge-two-sorted-lists.py @@ -0,0 +1,21 @@ +# https://leetcode.com/problems/merge-two-sorted-lists/ +# 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]: + if not list1 and not list2: + return None + if not list1: + return list2 + if not list2: + return list1 + + if list1.val <= list2.val: + list1.next = self.mergeTwoLists(list1.next, list2) + return list1 + else: + list2.next = self.mergeTwoLists(list1, list2.next) + return list2