1
+ # 143. Reorder List
2
+ # 🟠 Medium
3
+ #
1
4
# https://leetcode.com/problems/reorder-list/
2
-
5
+ #
3
6
# Tags: Linked List - Two Pointers - Stack - Recursion
4
7
5
8
import timeit
9
12
from data import ListNode , deserializeListToLinkedList , serializeLinkedList
10
13
11
14
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.
13
17
#
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.
16
20
#
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%
19
23
class Deque :
20
24
def reorderList (self , head : Optional [ListNode ]) -> None :
21
25
"""
@@ -39,14 +43,15 @@ def reorderList(self, head: Optional[ListNode]) -> None:
39
43
current .next = None
40
44
41
45
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.
44
49
#
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.
47
52
#
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%
50
55
class NoExtraMemory :
51
56
def _mergeLists (self , head_a , head_b ):
52
57
tail = head_a
@@ -100,17 +105,18 @@ def test():
100
105
]
101
106
for executor in executors :
102
107
start = timeit .default_timer ()
103
- for _ in range (int ( float ( "1" )) ):
108
+ for _ in range (1 ):
104
109
for col , t in enumerate (tests ):
105
110
sol = executor ()
106
111
head = deserializeListToLinkedList (t [0 ])
107
112
# Modify in place
108
113
sol .reorderList (head )
109
114
exp = t [1 ]
110
115
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
+ )
114
120
stop = timeit .default_timer ()
115
121
used = str (round (stop - start , 5 ))
116
122
res = "{0:20}{1:10}{2:10}" .format (executor .__name__ , used , "seconds" )
0 commit comments