-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathmerge_two_list.py
More file actions
77 lines (61 loc) · 1.83 KB
/
merge_two_list.py
File metadata and controls
77 lines (61 loc) · 1.83 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Merge Two Sorted Lists
Merge two sorted linked lists into a single sorted list by splicing together
the nodes of the two input lists.
Reference: https://leetcode.com/problems/merge-two-sorted-lists/
Complexity:
Time: O(m + n)
Space: O(1) iterative, O(m + n) recursive
"""
from __future__ import annotations
class Node:
def __init__(self, x: int) -> None:
self.val = x
self.next: Node | None = None
def merge_two_list(l1: Node | None, l2: Node | None) -> Node | None:
"""Merge two sorted linked lists iteratively.
Args:
l1: Head of the first sorted list.
l2: Head of the second sorted list.
Returns:
Head of the merged sorted list.
Examples:
>>> a = Node(1); a.next = Node(3)
>>> b = Node(2); b.next = Node(4)
>>> result = merge_two_list(a, b)
>>> result.val
1
"""
sentinel = current = Node(0)
while l1 and l2:
if l1.val < l2.val:
current.next = l1
l1 = l1.next
else:
current.next = l2
l2 = l2.next
current = current.next
current.next = l1 or l2
return sentinel.next
def merge_two_list_recur(l1: Node | None, l2: Node | None) -> Node | None:
"""Merge two sorted linked lists recursively.
Args:
l1: Head of the first sorted list.
l2: Head of the second sorted list.
Returns:
Head of the merged sorted list.
Examples:
>>> a = Node(1); a.next = Node(3)
>>> b = Node(2); b.next = Node(4)
>>> result = merge_two_list_recur(a, b)
>>> result.val
1
"""
if not l1 or not l2:
return l1 or l2
if l1.val < l2.val:
l1.next = merge_two_list_recur(l1.next, l2)
return l1
else:
l2.next = merge_two_list_recur(l1, l2.next)
return l2