An interactive visualization of the 3-phase linked list reorder algorithm. Watch how slow/fast pointers find the middle, the second half is reversed in place, and then the two halves are interleaved.
Linked List LeetCode 143 โ MediumL0 โ L1 โ โฆ โ Ln-1 โ LnL0 โ Ln โ L1 โ Ln-1 โ L2 โ Ln-2 โ โฆfun reorderList(head: ListNode?) { // Phase 1: Find middle var slow = head; var fast = head?.next while (fast != null && fast.next != null) { slow = slow?.next fast = fast.next?.next } // Phase 2: Reverse second half var second = slow?.next slow?.next = null var prev: ListNode? = null while (second != null) { val tmp = second.next second.next = prev prev = second second = tmp } // Phase 3: Merge two halves var first = head; second = prev while (second != null) { val tmp1 = first?.next; val tmp2 = second.next first?.next = second second.next = tmp1 first = tmp1; second = tmp2 } }
This problem elegantly combines three classic linked list operations into one algorithm: finding the middle (slow/fast pointers), reversing a linked list, and merging two lists. By splitting the list, reversing the back half, and interleaving, we achieve the reorder in O(1) extra space without needing an array or stack.
slow starts at head, fast starts at head.next. Move slow by 1 and fast by 2 each step. When fast reaches the end, slow is at the middle node.slow.next, reverse the second half of the list in place using the standard three-pointer reversal (prev, curr, next). Disconnect the two halves by setting slow.next = None.Each phase traverses the list (or half of it) once, giving O(n) total time. We only use a constant number of pointer variables โ no additional data structures are needed.