Reorder a list: L0, Ln, L1, Ln-1, …
Rearrange a list into the zigzag order L0, Ln, L1, Ln-1, … by splitting it in half, reversing the back half, and weaving the two together.
Watch it animatedStep through it, with the running line highlighted. Free, no sign-in needed to watch.The idea
Find the middle, cut the list there, reverse the second half, then zip the two halves together one node at a time, alternating first half, second half. That turns a straight list into L0, Ln, L1, Ln-1, … without any extra memory.
Code
public static void Reorder(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; }
ListNode second = Reverse(slow.next);
slow.next = null;
ListNode p1 = head, p2 = second;
while (p2 != null) {
ListNode n1 = p1.next, n2 = p2.next;
p1.next = p2; p2.next = n1;
p1 = n1; p2 = n2;
}
}static void reorder(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; }
ListNode second = reverse(slow.next);
slow.next = null;
ListNode p1 = head, p2 = second;
while (p2 != null) {
ListNode n1 = p1.next, n2 = p2.next;
p1.next = p2; p2.next = n1;
p1 = n1; p2 = n2;
}
}void reorderList(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) { slow = slow->next; fast = fast->next->next; }
ListNode* second = reverseList(slow->next);
slow->next = nullptr;
ListNode *p1 = head, *p2 = second;
while (p2) {
ListNode *n1 = p1->next, *n2 = p2->next;
p1->next = p2; p2->next = n1;
p1 = n1; p2 = n2;
}
}def reorder(head: ListNode) -> None:
slow = fast = head
while fast and fast.next: slow = slow.next; fast = fast.next.next
second = reverse(slow.next)
slow.next = None
p1, p2 = head, second
while p2:
n1, n2 = p1.next, p2.next
p1.next = p2; p2.next = n1
p1, p2 = n1, n2Tips
- Cut with slow.next = null before reversing, or the reversal walks straight past the cut into the part you already handled.
- The weave loop only has to check p2, because the first half is never shorter than the second.
In the .NET library
This chains three classic sub-patterns: fast/slow to find the middle, in-place reversal, and a merge that alternates by position instead of by value.