-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm143.py
32 lines (29 loc) · 852 Bytes
/
m143.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
# 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: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
toReadd = []
curr = head
while curr :
toReadd.append(curr)
curr = curr.next
curr = head
numNodes = len(toReadd)
last = head
while numNodes > 1 :
toReadd[-1].next = curr.next
curr.next = toReadd.pop()
last = curr.next
curr = curr.next.next
numNodes -= 2
if numNodes == 0 :
last.next = None
else :
last.next = toReadd.pop()
last.next.next = None