-
Notifications
You must be signed in to change notification settings - Fork 0
/
57_Reorder_List
39 lines (34 loc) · 1001 Bytes
/
57_Reorder_List
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
# 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 or not head.next.next: return
# Find the middle of the list
slow, fast=head, head
while fast.next and fast.next.next:
slow=slow.next
fast=fast.next.next
# Reverse the second half of the list
prev=None
cur=slow.next
while cur:
Next=cur.next
cur.next=prev
prev=cur
cur=Next
slow.next=None
# Merge the 2 halves
A, B=head, prev
while A and B:
A_next=A.next
B_next=B.next
A.next=B
B.next=A_next
A=A_next
B=B_next