forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
143.py
41 lines (35 loc) · 945 Bytes
/
143.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
36
37
38
39
40
41
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reorderList(self, head):
"""
:type head: ListNode
:rtype: void Do not return anything, modify head in-place instead.
"""
if not head:
return None
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
head1, head2 = head, slow.next
slow.next = None
prev = None
curr = head2
while curr:
nex = curr.next
curr.next = prev
prev = curr
curr = nex
head2 = prev
while head2:
n1 = head1.next
n2 = head2.next
head1.next = head2
head1.next.next = n1
head2 = n2
head1 = head1.next.next
head = head1