-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreorderList.py
38 lines (32 loc) · 914 Bytes
/
reorderList.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
slow = head
fast = head
while(fast and fast.next):
slow = slow.next
fast = fast.next.next
prev = None
curr = slow
fwd = slow
while(curr is not None):
fwd = curr.next
curr.next = prev
prev = curr
curr = fwd
last = prev
curr = head
while(last.next is not None):
fwd = curr.next
curr.next = last
lastFwd = last.next
last.next = fwd
curr = fwd
last = lastFwd