-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathreorder-list.py
50 lines (37 loc) · 1.13 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import math
from typing import Optional
# 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.
"""
def reverse_list(node: Optional[ListNode]) -> Optional[ListNode]:
prev = None
while node:
node_next = node.next
node.next = prev
prev = node
node = node_next
return prev
if not head:
return
num_nodes = 0
node = head
while node:
num_nodes += 1
node = node.next
node = head
for _ in range(math.ceil(num_nodes / 2) - 1):
node = node.next
last = reverse_list(node.next)
node.next = None
node = head
while node and last:
node_new, last_new = node.next, last.next
node.next, last.next = last, node.next
node, last = node_new, last_new