-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorder_list.py
43 lines (30 loc) · 1.04 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
# https://leetcode.com/problems/reorder-list/description/
# git add . && git commit -m "completed reorder_list" && git push && exit
# Definition for singly-linked list.
from collections import deque
from typing import Deque
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.
"""
node_stack: Deque = deque()
start = head
end = head
while end != None and end.next != None:
end = end.next.next
start = start.next
while start != None:
node_stack.append(start)
start = start.next
current_node = head
while len(node_stack) != 0:
popped_node = node_stack.pop()
popped_node.next = current_node.next
current_node.next = popped_node
current_node = popped_node.next
current_node.next = None