-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
28 lines (23 loc) · 775 Bytes
/
solution.py
File metadata and controls
28 lines (23 loc) · 775 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
# 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
reverse_sequence = []
while head:
reverse_sequence.append(head)
head = head.next
sequence = reverse_sequence[::-1]
length = len(sequence)
p = ListNode(-1)
for i in xrange(length):
p.next = sequence.pop() if i % 2 == 0 else reverse_sequence.pop()
p = p.next
p.next = None