-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathReorderList.py
More file actions
60 lines (56 loc) · 1.34 KB
/
ReorderList.py
File metadata and controls
60 lines (56 loc) · 1.34 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @return nothing
def len(self,head):
cnt = 0
p=head
while p:
cnt+=1
p=p.next
return cnt
def advance(self,head,n):
p=head
for x in xrange(1,n):
p=p.next
return p
def reverse(self,head):
if not head:
return head
next=head.next
head.next=None
while next:
nnext=next.next
next.next=head
head=next
next=nnext
return head
def interleave(self,l0,l1):
if not l0: return l1
if not l1: return l0
head=l0
p0,p1=l0.next,l1.next
head.next=l1
p=l1
while p0 and p1:
p00,p11=p0.next,p1.next
p.next=p0
p0.next=p1
p=p1
p0,p1=p00,p11
if not p0: p.next=p1
if not p1: p.next=p0
return head
def reorderList(self, head):
if head is None:
return
n=self.len(head)
mid=self.advance(head,n-n/2)
l0=head
l1=self.reverse(mid.next)
mid.next=None
head=self.interleave(l0,l1)