Skip to content

Commit e153484

Browse files
authored
Merge pull request #140 from WazedKhan/leetcode-143-reorder-list
LeetCode #143: Reorder List
2 parents 32d4ab4 + 591bf19 commit e153484

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Optional
2+
3+
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val = val
7+
self.next = next
8+
9+
10+
class Solution:
11+
def reorderList(self, head: Optional[ListNode]) -> None:
12+
slow, fast = head, head
13+
14+
while fast and fast.next:
15+
slow = slow.next
16+
fast = fast.next.next
17+
half = slow.next
18+
slow.next = None
19+
20+
prev = None
21+
while half:
22+
next_node = half.next
23+
half.next = prev
24+
prev = half
25+
half = next_node
26+
27+
while head and prev:
28+
next_head = head.next
29+
head.next = prev
30+
next_prev = prev.next
31+
prev.next = next_head
32+
head = next_head
33+
prev = next_prev

0 commit comments

Comments
 (0)