-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3. Reorder List.py
35 lines (30 loc) · 922 Bytes
/
3. Reorder List.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
# 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, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
mid = slow
first = head
curr, prev = mid, None
while curr:
temp = curr.next
curr.next = prev
prev = curr
curr = temp
second = prev
while second.next:
temp1 = first.next
first.next = second
first = temp1
temp2 = second.next
second.next = first
second = temp2