From 5cafe951dbc6c2434068ffc0abc8b391228505c7 Mon Sep 17 00:00:00 2001 From: pro100ton Date: Fri, 14 Feb 2025 22:04:42 +0300 Subject: [PATCH] Add merge_two_linked_lists solution --- .../merge_two_linked_lists/README.md | 143 ++++++++++++++++++ .../merge_two_linked_lists/main.py | 76 ++++++++++ .../neetcode_solution.py | 49 ++++++ 3 files changed, 268 insertions(+) create mode 100644 neetcode/linked_list/merge_two_linked_lists/README.md create mode 100644 neetcode/linked_list/merge_two_linked_lists/main.py create mode 100644 neetcode/linked_list/merge_two_linked_lists/neetcode_solution.py diff --git a/neetcode/linked_list/merge_two_linked_lists/README.md b/neetcode/linked_list/merge_two_linked_lists/README.md new file mode 100644 index 0000000..0c92f41 --- /dev/null +++ b/neetcode/linked_list/merge_two_linked_lists/README.md @@ -0,0 +1,143 @@ +Merge Two Sorted Linked Lists +You are given the heads of two sorted linked lists list1 and list2. + +Merge the two lists into one sorted linked list and return the head of the new sorted linked list. + +The new list should be made up of nodes from list1 and list2. + +Example 1: + + + +Input: list1 = [1,2,4], list2 = [1,3,5] + +Output: [1,1,2,3,4,5] +Example 2: + +Input: list1 = [], list2 = [1,2] + +Output: [1,2] +Example 3: + +Input: list1 = [], list2 = [] + +Output: [] +Constraints: + +0 <= The length of the each list <= 100. +-100 <= Node.val <= 100 + +# Solution + +## Edge cases +### Two empty +l1 = [] +l2 = [] + +r = None +### l1 empty +l1 = [] +l2 = [1,2,...,n] + +r = l2 +### l2 empty +l1 = [1,2,...,n] +l2 = [] + +r = l1 + +## Algorithm +### 0 + i +l1 = [1,2,4] + + j +l2 = [1,3,5] + +r = [] + +### 1 +Before: + + i +l1 = [1,2,4] + + j +l2 = [1,3,5] + +r = [] + +Step: +``` +if i.val <= j.val: + r = i + i = i.next +``` + +After: + + i +l1 = [1,2,4] + + j +l2 = [1,3,5] + +r = [l10] + +### 2 +Before: + i +l1 = [1,2,4] + + j +l2 = [1,3,5] + +r = [l10] + +Step: +``` +if i.val <= j.val: + --- +else: + r.next = j + j = j.next + r = r.next +``` +After: + i +l1 = [1,2,4] + + j +l2 = [1,3,5] + +r = [l10, l20] + +### 3 +Before: + i +l1 = [1,2,4] + + j +l2 = [1,3,5] + +r = [l10, l20] + +Step: +``` +if i.val <= j.val: + r.next = i + i = i.next + r = r.next +else: + r.next = j + j = j.next + r = r.next +``` +After: + j +l1 = [1,2,4] + + j +l2 = [1,3,5] + +r = [l10, l20, l11] diff --git a/neetcode/linked_list/merge_two_linked_lists/main.py b/neetcode/linked_list/merge_two_linked_lists/main.py new file mode 100644 index 0000000..fae4cda --- /dev/null +++ b/neetcode/linked_list/merge_two_linked_lists/main.py @@ -0,0 +1,76 @@ +from typing import Optional + + +# 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 and isinstance(list2, ListNode): + return list2 + if not list2 and isinstance(list1, ListNode): + return list1 + i = list1 + j = list2 + r = None + rf = None + while i or j: + print(f"i : {i.val if i else 'No value'}\nj : {j.val if j else 'No value'}\n") + if not j: + r.next = i + i = i.next + r = r.next + continue + if not i: + r.next = j + j = j.next + r = r.next + continue + if i.val <= j.val: + if not r: + r = i + rf = i + else: + r.next = i + r = r.next + i = i.next + else: + if not r: + r = j + rf = j + else: + r.next = j + r = r.next + j = j.next + return rf + + +if __name__ == "__main__": + s = Solution() + # l12 = ListNode(val=4, next=None) + # l11 = ListNode(val=2, next=l12) + # l10 = ListNode(val=1, next=l11) + # + # l22 = ListNode(val=5, next=None) + # l21 = ListNode(val=3, next=l22) + # l20 = ListNode(val=1, next=l21) + + l11 = ListNode(val=3, next=None) + l10 = ListNode(val=-9, next=l11) + + l21 = ListNode(val=7, next=None) + l20 = ListNode(val=5, next=l21) + + k = s.mergeTwoLists(list1=l10, list2=l20) + + while k: + print(f"{k.val}") + k = k.next diff --git a/neetcode/linked_list/merge_two_linked_lists/neetcode_solution.py b/neetcode/linked_list/merge_two_linked_lists/neetcode_solution.py new file mode 100644 index 0000000..b1ef73b --- /dev/null +++ b/neetcode/linked_list/merge_two_linked_lists/neetcode_solution.py @@ -0,0 +1,49 @@ +from typing import Optional + + +# 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: 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 + + +if __name__ == "__main__": + s = Solution() + # l12 = ListNode(val=4, next=None) + # l11 = ListNode(val=2, next=l12) + # l10 = ListNode(val=1, next=l11) + # + # l22 = ListNode(val=5, next=None) + # l21 = ListNode(val=3, next=l22) + # l20 = ListNode(val=1, next=l21) + + l11 = ListNode(val=3, next=None) + l10 = ListNode(val=-9, next=l11) + + l21 = ListNode(val=7, next=None) + l20 = ListNode(val=5, next=l21) + + k = s.mergeTwoLists(list1=l10, list2=l20) + + while k: + print(f"{k.val}") + k = k.next