Reorder List
Find middle → Reverse second half → Merge alternately
Reorder List (Python)
1class Solution:● def reorderList(self, head):3 if not head or not head.next:......8 while fast.next and fast.next.next:9 slow = slow.next10 fast = fast.next.next11 ......13 prev, curr = None, slow.next14 slow.next = None15 while curr:16 tmp = curr.next17 curr.next = prev18 prev = curr19 curr = tmp......21 # Phase 3: Merge two halves22 p1, p2 = head, prev23 while p2:24 p1_next, p2_next = p1.next, p2.next25 p1.next = p226 p2.next = p1_next27 p1 = p1_next28 p2 = p2_nextCurrent Line (2): Method Signature
Reorder List Visualizer
Start
Step
0 / 29
Phase
Find Middle
Nodes
5
Reorder Progress
Pointer State
Step Explanation
StartInitialize Pointers
Set slow and fast pointers both to head. We'll use slow/fast to find the midpoint of the list.
- > Phase: Find Middle
- > slow and fast both start at head.
- > slow moves 1 step at a time; fast moves 2 — when fast hits the end, slow is at the middle.
Unvisited
Current (slow/p1)
Prev
Fast / p2 / tmp
Reversed
Done / Merged