-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path143-Reorder-List.py
More file actions
32 lines (29 loc) · 911 Bytes
/
143-Reorder-List.py
File metadata and controls
32 lines (29 loc) · 911 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
# 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.
\\\
if not head or not head.next:
return
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
prev, curr = None, slow.next
slow.next = None
while curr:
temp = curr.next
curr.next = prev
prev = curr
curr = temp
first, second = head, prev
while second:
temp1, temp2 = first.next, second.next
first.next = second
second.next = temp1
first, second = temp1, temp2