Skip to content

Commit df227de

Browse files
authored
Create 143-Reorder-List.py
1 parent 9b9cf1d commit df227de

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

143-Reorder-List.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def reorderList(self, head: ListNode) -> None:
3+
# find middle
4+
slow, fast = head, head.next
5+
while fast and fast.next:
6+
slow = slow.next
7+
fast = fast.next.next
8+
9+
# reverse second half
10+
second = slow.next
11+
prev = slow.next = None
12+
while second:
13+
tmp = second.next
14+
second.next = prev
15+
prev = second
16+
second = tmp
17+
18+
# merge two halfs
19+
first, second = head, prev
20+
while second:
21+
tmp1, tmp2 = first.next, second.next
22+
first.next = second
23+
second.next = tmp1
24+
first, second = tmp1, tmp2

0 commit comments

Comments
 (0)