We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 32d4ab4 + 591bf19 commit e153484Copy full SHA for e153484
1 file changed
LeetCode/medium/reorder_list_143.py
@@ -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