-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge-two-sorted-lists.py
More file actions
52 lines (44 loc) · 1.58 KB
/
merge-two-sorted-lists.py
File metadata and controls
52 lines (44 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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:
# Time complexity: O(n + m) where n and m are the length of list1 and list2
# Space complexity: O(n + m)
# recursive approach
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
def merge(l1, l2, res):
if not l1:
res.next = l2
elif not l2:
res.next = l1
else:
if l1.val <= l2.val:
res.next = l1
l1 = l1.next
else:
res.next = l2
l2 = l2.next
merge(l1, l2, res.next)
return res
return merge(list1, list2, ListNode()).next
# Time complexity: O(n + m) where n and m are the length of list1 and list2
# Space complexity: O(1)
# Iterative approach
def mergeTwoLists2(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy_head = curr = ListNode()
while list1 and list2:
if list1.val <= list2.val:
curr.next = list1
list1 = list1.next
else:
curr.next = list2
list2 = list2.next
curr = curr.next
if list1:
curr.next = list1
elif list2:
curr.next = list2
return dummy_head.next