Add merge_two_linked_lists solution

This commit is contained in:
pro100ton 2025-02-14 22:04:42 +03:00
parent 8bf8740e48
commit 5cafe951db
3 changed files with 268 additions and 0 deletions

View file

@ -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]

View file

@ -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

View file

@ -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