-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path143_Reorder_List.py
More file actions
33 lines (32 loc) · 1.21 KB
/
143_Reorder_List.py
File metadata and controls
33 lines (32 loc) · 1.21 KB
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(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reorderList(self, head):
"""
:type head: ListNode
:rtype: None Do not return anything, modify head in-place instead.
"""
# find the middle point of the linked list
slow, fast = head, head.next
while fast and fast.next:
fast = fast.next.next
slow = slow.next
second_half = slow.next
slow.next = None # to break the linked list into first & second half
# reverse the second half of the linked list
reversed_second_half = None
while second_half:
tmp = second_half.next
second_half.next = reversed_second_half
reversed_second_half = second_half
second_half = tmp
first_half, second_half = head, reversed_second_half
# merge first & second half alternatively
while second_half:
tmp1, tmp2 = first_half.next, second_half.next
first_half.next = second_half
second_half.next = tmp1
first_half, second_half = tmp1, tmp2