forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamthekorean.py
32 lines (27 loc) ยท 1009 Bytes
/
samthekorean.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
# TC : O(n)
# SC : O(m) m being the sum of deque and dummy nodes
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
if not head or not head.next or not head.next.next:
return
# Step 1: Use a deque to collect nodes
d = deque()
current = head
while current:
d.append(current)
current = current.next
# Step 2: Reorder the list using deque
dummy = ListNode(0) # Dummy node with value 0
current = dummy
toggle = True
while d:
if toggle:
current.next = d.popleft() # Append from the front of the deque
else:
current.next = d.pop() # Append from the back of the deque
current = current.next
toggle = not toggle
# Step 3: Ensure the last node points to None
current.next = None
# Step 4: Reassign head to point to the new reordered list
head = dummy.next