Skip to content

Commit e1c7e23

Browse files
committed
LeetCode-143: Reorder List
1 parent faf9822 commit e1c7e23

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

prep/practice/linked_list.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,28 @@ def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNod
7676
current = current.next
7777
current.next = current.next.next
7878
return dummy.next
79+
80+
def reorderList(self, head: Optional[ListNode]) -> None:
81+
slow, fast = head, head
82+
83+
while fast and fast.next:
84+
slow = slow.next
85+
fast = fast.next.next
86+
half = slow.next
87+
slow.next = None
88+
89+
prev = None
90+
while half:
91+
next_node = half.next
92+
half.next = prev
93+
prev = half
94+
half = next_node
95+
96+
while head and prev:
97+
nxt_head = head.next
98+
head.next = prev
99+
nxt_prev = prev.next
100+
prev.next = nxt_head
101+
102+
head = nxt_head
103+
prev = nxt_prev

0 commit comments

Comments
 (0)