Optimal: split, reverse, weave
Time O(n) Space O(1)Find the end of the first half (first middle). Cut there and reverse the second half. Then alternate: take one node from the first half, one from the reversed second half, until the second half runs out.
head = [1, 2, 3, 4, 5]Step 1/3Split after the middle: first half 1 → 2 → 3, second half 4 → 5.
class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head, fast = head;
while (fast.next != null && fast.next.next != null) { // slow ends at the first middle
slow = slow.next;
fast = fast.next.next;
}
ListNode second = slow.next;
slow.next = null; // cut
ListNode prev = null; // reverse the second half
while (second != null) {
ListNode next = second.next;
second.next = prev;
prev = second;
second = next;
}
ListNode a = head, b = prev; // weave
while (b != null) {
ListNode an = a.next, bn = b.next;
a.next = b;
b.next = an;
a = an;
b = bn;
}
}
}Verdict: Three linear passes, no extra memory.