-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC143_ReorderList.py
33 lines (30 loc) · 1010 Bytes
/
LC143_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
# 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.
"""
# find the middle of the list
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# reverse second half of the list
second = slow.next
prev = slow.next = None
while second:
tmp = second.next
second.next = prev
prev = second
second = tmp
# merge two halves
first, second = head, prev
while second:
tmp_first, tmp_second = first.next, second.next
first.next = second
second.next = tmp_first
first, second = tmp_first, tmp_second