From b8beb67e6b958d64b6f1816bd5b2f254982932b4 Mon Sep 17 00:00:00 2001 From: Chris Cooper Date: Sun, 3 Sep 2023 15:19:38 -0700 Subject: [PATCH 1/3] Use recursion to minimize the code and it is surprisingly fast. --- python/0021-merge-two-sorted-lists.py | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/python/0021-merge-two-sorted-lists.py b/python/0021-merge-two-sorted-lists.py index 9f6cd8044..2626af4e0 100644 --- a/python/0021-merge-two-sorted-lists.py +++ b/python/0021-merge-two-sorted-lists.py @@ -4,22 +4,11 @@ # self.val = val # self.next = next class Solution: - def mergeTwoLists(self, list1: ListNode, list2: ListNode) -> 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 - elif list2: - tail.next = list2 - - return dummy.next + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + if not list1: + return list2 + if not list2: + return list1 + lil, big = (list1, list2) if list1.val < list2.val else (list2, list1) + lil.next = self.mergeTwoLists(lil.next, big) + return lil From cb95cde111e4c79532fd2228c26ba02abdead852 Mon Sep 17 00:00:00 2001 From: Chris Cooper Date: Mon, 4 Sep 2023 09:00:32 -0700 Subject: [PATCH 2/3] Add Iterative along with Recursive solution --- python/0021-merge-two-sorted-lists.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/python/0021-merge-two-sorted-lists.py b/python/0021-merge-two-sorted-lists.py index 2626af4e0..edbf54db0 100644 --- a/python/0021-merge-two-sorted-lists.py +++ b/python/0021-merge-two-sorted-lists.py @@ -3,6 +3,8 @@ # def __init__(self, val=0, next=None): # self.val = val # self.next = next + +# Recursive class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: if not list1: @@ -12,3 +14,21 @@ def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> lil, big = (list1, list2) if list1.val < list2.val else (list2, list1) lil.next = self.mergeTwoLists(lil.next, big) return lil + +# Iterative +class Solution: + def mergeTwoLists(self, list1: ListNode, list2: ListNode) -> ListNode: + dummy = node = ListNode() + + while list1 and list2: + if list1.val < list2.val: + node.next = list1 + list1 = list1.next + else: + node.next = list2 + list2 = list2.next + node = node.next + + node.next = list1 or list2 + + return dummy.next From 3e8f1a23cb963f57f1cd11f5c35e689f1c35bedd Mon Sep 17 00:00:00 2001 From: Chris Cooper Date: Mon, 4 Sep 2023 09:04:29 -0700 Subject: [PATCH 3/3] Moved the original solution to the top and the new one to the bottom. --- python/0021-merge-two-sorted-lists.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/python/0021-merge-two-sorted-lists.py b/python/0021-merge-two-sorted-lists.py index edbf54db0..e08821bda 100644 --- a/python/0021-merge-two-sorted-lists.py +++ b/python/0021-merge-two-sorted-lists.py @@ -4,17 +4,6 @@ # self.val = val # self.next = next -# Recursive -class Solution: - def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: - if not list1: - return list2 - if not list2: - return list1 - lil, big = (list1, list2) if list1.val < list2.val else (list2, list1) - lil.next = self.mergeTwoLists(lil.next, big) - return lil - # Iterative class Solution: def mergeTwoLists(self, list1: ListNode, list2: ListNode) -> ListNode: @@ -32,3 +21,14 @@ def mergeTwoLists(self, list1: ListNode, list2: ListNode) -> ListNode: node.next = list1 or list2 return dummy.next + +# Recursive +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + if not list1: + return list2 + if not list2: + return list1 + lil, big = (list1, list2) if list1.val < list2.val else (list2, list1) + lil.next = self.mergeTwoLists(lil.next, big) + return lil