-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathreorderList.py
More file actions
30 lines (29 loc) · 802 Bytes
/
reorderList.py
File metadata and controls
30 lines (29 loc) · 802 Bytes
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param A : head node of linked list
# @return the head node in the linked list
def reorderList(self, A):
arr = []
ca = A
alen = 0
while ca:
arr.append(ca)
alen += 1
ca = ca.next
cn = arr[0]
for i in xrange(1, alen):
if i%2 == 0:
cn.next = arr[i/2]
cn = cn.next
if i == alen - 1:
cn.next = None
else:
cn.next = arr[alen - i/2 - 1]
cn = cn.next
if i == alen - 1:
cn.next = None
return A