-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreorder_list.py
More file actions
32 lines (26 loc) · 891 Bytes
/
reorder_list.py
File metadata and controls
32 lines (26 loc) · 891 Bytes
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
from typing import Optional
from src.common import ListNode
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
if not head or not head.next or not head.next.next:
return
# Step 1: Find the middle of the list
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# Step 2: Reverse the second half of the list
prev, curr = None, slow.next
slow.next = None
while curr:
temp = curr.next
curr.next = prev
prev = curr
curr = temp
# Step 3: Merge the two halves
first, second = head, prev
while second:
temp1, temp2 = first.next, second.next
first.next = second
second.next = temp1
first, second = temp1, temp2