-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddle-of-the-linked-list.py
90 lines (80 loc) · 2.62 KB
/
middle-of-the-linked-list.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
# 876. Middle of the Linked List
# 🟢 Easy
#
# https://leetcode.com/problems/middle-of-the-linked-list/
#
# Tags: Linked List - Two Pointers
import timeit
from typing import Optional
from data import LinkedList, ListNode
# Use the fast&slow pointers technique, for each two moves of the fast
# pointer, we can move the slow pointer once.
#
# Time complexity: O(n) - We visit each node at most once.
# Space complexity: O(1) - Constant extra memory used.
#
# Runtime: 32 ms, faster than 91.11%
# Memory Usage: 13.8 MB, less than 95.69%
class Shuffle:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None
slow = fast = head
shuffle = True
while fast.next:
# Always move the fast pointer.
fast = fast.next
# Only move the slow pointer every second time.
if shuffle:
slow = slow.next
shuffle = not shuffle
return slow
# Use the fast&slow pointers technique, for each two moves of the fast
# pointer, we can move the slow pointer once.
#
# Time complexity: O(n) - We visit each node at most once.
# Space complexity: O(1) - Constant extra memory used.
#
# Runtime: 29 ms, faster than 96.88%
# Memory Usage: 13.7 MB, less than 95.57%
class FastAndSlow:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
# head is guaranteed to not be null.
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
return slow
def test():
executors = [
Shuffle,
FastAndSlow,
]
tests = [
[[1], [1]],
[[1, 2], [2]],
[[1, 2, 3], [2, 3]],
[[1, 2, 3], [2, 3]],
[[1, 2, 3, 4], [3, 4]],
[[1, 2, 3, 4, 5], [3, 4, 5]],
[[1, 2, 3, 4, 5, 6], [4, 5, 6]],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
head = LinkedList.fromList(t[0]).getHead()
middle = sol.middleNode(head)
result = LinkedList(middle).toList()
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()