Skip to content

Commit 1da9ef9

Browse files
committed
LC 143. Reorder List (Python) Revisit
1 parent 6e337f2 commit 1da9ef9

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

leetcode/reorder-list.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# 143. Reorder List
2+
# 🟠 Medium
3+
#
14
# https://leetcode.com/problems/reorder-list/
2-
5+
#
36
# Tags: Linked List - Two Pointers - Stack - Recursion
47

58
import timeit
@@ -9,13 +12,14 @@
912
from data import ListNode, deserializeListToLinkedList, serializeLinkedList
1013

1114

12-
# If we can use extra memory, store all nodes in a deque and pop alternatively from left/right reconnecting the nodes.
15+
# If we can use extra memory, store all nodes in a deque and pop
16+
# alternatively from left/right reconnecting the nodes.
1317
#
14-
# Time complexity: O(n) - we travel through the linked list, then the deque
15-
# Space complexity: O(n) - we store all ListNodes in the deque
18+
# Time complexity: O(n) - We travel through the linked list, then the deque.
19+
# Space complexity: O(n) - We store all ListNodes in the deque.
1620
#
17-
# Runtime: 112 ms, faster than 72.70% of Python3 online submissions for Reorder List.
18-
# Memory Usage: 24.1 MB, less than 28.29% of Python3 online submissions for Reorder List.
21+
# Runtime 56 ms Beats 54%
22+
# Memory 24.86 MB Beats 5%
1923
class Deque:
2024
def reorderList(self, head: Optional[ListNode]) -> None:
2125
"""
@@ -39,14 +43,15 @@ def reorderList(self, head: Optional[ListNode]) -> None:
3943
current.next = None
4044

4145

42-
# If are not allowed to use any extra memory, we can do it by finding the middle, reversing the second half of the
43-
# linked list, then merging the non-reversed first half with the second, reversed half.
46+
# If are not allowed to use any extra memory, we can do it by finding
47+
# the middle, reversing the second half of the linked list, then merging
48+
# the non-reversed first half with the second, reversed half.
4449
#
45-
# Time complexity: O(n) - we visit all nodes a linear number of times.
46-
# Space complexity; O(1) - only a fixed number of variables are kept.
50+
# Time complexity: O(n) - We visit all nodes a linear number of times.
51+
# Space complexity; O(1) - Only a fixed number of variables are kept.
4752
#
48-
# Runtime: 188 ms, faster than 10.57% of Python3 online submissions for Reorder List.
49-
# Memory Usage: 23.9 MB, less than 63.99% of Python3 online submissions for Reorder List.
53+
# Runtime 43 ms Beats 97%
54+
# Memory 24.1 MB Beats 50%
5055
class NoExtraMemory:
5156
def _mergeLists(self, head_a, head_b):
5257
tail = head_a
@@ -100,17 +105,18 @@ def test():
100105
]
101106
for executor in executors:
102107
start = timeit.default_timer()
103-
for _ in range(int(float("1"))):
108+
for _ in range(1):
104109
for col, t in enumerate(tests):
105110
sol = executor()
106111
head = deserializeListToLinkedList(t[0])
107112
# Modify in place
108113
sol.reorderList(head)
109114
exp = t[1]
110115
serialized_result = serializeLinkedList(head)
111-
assert (
112-
serialized_result == exp
113-
), f"\033[93m» {serialized_result} <> {exp}\033[91m for test {col} using \033[1m{executor.__name__}"
116+
assert serialized_result == exp, (
117+
f"\033[93m» {serialized_result} <> {exp}\033[91m for"
118+
+ f" test {col} using \033[1m{executor.__name__}"
119+
)
114120
stop = timeit.default_timer()
115121
used = str(round(stop - start, 5))
116122
res = "{0:20}{1:10}{2:10}".format(executor.__name__, used, "seconds")

0 commit comments

Comments
 (0)