-
Notifications
You must be signed in to change notification settings - Fork 0
/
143_reorder_list.py
31 lines (31 loc) · 934 Bytes
/
143_reorder_list.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if head == None:
return head
temp = head.next
doubleEnded = collections.deque([])
while temp:
doubleEnded.append(temp)
temp = temp.next
popRight = True
temp = head
while len(doubleEnded) > 0:
if popRight:
temp2 = doubleEnded.pop()
temp.next = temp2
temp = temp2
popRight = False
else:
temp2 = doubleEnded.popleft()
temp.next = temp2
temp = temp2
popRight = True
temp.next = None