-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143.py
41 lines (37 loc) · 1.02 KB
/
143.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
33
34
35
36
37
38
39
40
41
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
if head == None:
return None
# find mid
slow, fast = head, head
while True:
if fast.next == None:
break
fast = fast.next
if fast.next == None:
break
fast = fast.next
if slow.next == None:
break
slow = slow.next
second = slow.next
slow.next = None
# reverse second part
curr = second
prev = None
while curr:
next = curr.next
curr.next = prev
prev = curr
curr = next
# reorder
second = prev
first = head
while first and second:
firstNext = first.next
secondNext = second.next
first.next = second
first = first.next
first.next = firstNext
first = first.next
second = secondNext