Skip to content

Commit 6c45667

Browse files
committed
feat: Solve reorder-list problem
1 parent b42cc9e commit 6c45667

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

reorder-list/hu6r1s.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def reorderList(self, head: Optional[ListNode]) -> None:
8+
"""
9+
Do not return anything, modify head in-place instead.
10+
"""
11+
stack = []
12+
node = head
13+
while node:
14+
stack.append(node)
15+
node = node.next
16+
17+
node = dummy = ListNode(-1)
18+
for i in range(len(stack)):
19+
if i % 2:
20+
node.next = stack.pop()
21+
else:
22+
node.next = head
23+
head = head.next
24+
node = node.next
25+
node.next = None
26+
return dummy.next

0 commit comments

Comments
 (0)