Reorder List
Medium
Topics
Given the head of a singly linked list L0 → L1 → ... → Ln-1 → Ln, reorder it to L0 → Ln → L1 → Ln-1 → L2 → .... Return the reordered list. (LeetCode mutates in place and returns void; here return the head.)
Example 1
before
after
pulled from the back
The result alternates front, back, front, back.
Linked list values from head to tail.
Input: head = [1,2,3,4] Output: [1,4,2,3]
Example 2
before
after
pulled from the back
Input: head = [1,2,3,4,5] Output: [1,5,2,4,3]
Constraints
- 1 <= number of nodes <= 5*10^4
- 0 <= Node.val <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
The target order interleaves the list with its own reverse. Classic approach: find the middle, reverse the second half, then weave the two halves together.
Approach
- Find the midpoint with slow/fast pointers.
- Reverse the second half.
- Merge the first and reversed-second halves alternately.
Dry Run
list 1 -> 2 -> 3 -> 4 -> 5. Four phases, each a standard move.
1 find the midpoint with slow and fast slow stops on 3
2 CUT: first half 1 -> 2 -> 3
second half 4 -> 5
3 reverse the second half 5 -> 4
4 weave alternately 1 then 5, then 2 then 4
answer 1 -> 5 -> 2 -> 4 -> 3
The cut in phase 2 is the line that cannot be skipped. Without `slow.next = None`
the first half still points into the second, and once the second half is reversed
those two pointers close a loop -- on [1,2,3,4] the weave then never terminates,
which measures as a hang rather than a wrong answer.
`fast` starts at `head.next` rather than `head`, which decides where slow lands on
an odd-length list. Here slow stops on 3 so the first half gets the extra node,
which is what leaves the 3 at the END of the output.
The weave saves both successors before re-pointing anything -- `f2` and `s2` --
for the same reason reverse-linked-list saves `nxt`: the next statement destroys
the pointer it would otherwise need.Complexity
- Time — O(n): find-middle, reverse, and merge are each linear.
- Space — O(1): pointer surgery, no extra list.
Common Pitfalls
- Cut the first half (slow.next = None) before reversing, or you create a cycle.
Full Solution — all languages
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reorderList(self, head):
if not head or not head.next: return head
slow,fast=head,head.next
while fast and fast.next:
slow=slow.next; fast=fast.next.next
second=slow.next; slow.next=None
prev=None
while second:
nxt=second.next; second.next=prev; prev=second; second=nxt
first,second=head,prev
while second:
f2,s2=first.next,second.next
first.next=second; second.next=f2
first,second=f2,s2
return headimport java.util.*;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode reorderList(ListNode head) {
if(head==null||head.next==null)return head;
ListNode slow=head,fast=head.next;
while(fast!=null&&fast.next!=null) {
slow=slow.next;
fast=fast.next.next;
}
ListNode second=slow.next;
slow.next=null;
ListNode prev=null;
while(second!=null) {
ListNode nxt=second.next;
second.next=prev;
prev=second;
second=nxt;
}
ListNode first=head;
second=prev;
while(second!=null) {
ListNode f2=first.next,s2=second.next;
first.next=second;
second.next=f2;
first=f2;
second=s2;
}
return head;
}
}#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(nullptr) {
}
};
class Solution {
public: ListNode* reorderList(ListNode* head) {
if(!head||!head->next)return head;
ListNode* slow=head;
ListNode* fast=head->next;
while(fast&&fast->next) {
slow=slow->next;
fast=fast->next->next;
}
ListNode* second=slow->next;
slow->next=nullptr;
ListNode* prev=nullptr;
while(second) {
ListNode* nxt=second->next;
second->next=prev;
prev=second;
second=nxt;
}
ListNode* first=head;
second=prev;
while(second) {
ListNode* f2=first->next;
ListNode* s2=second->next;
first->next=second;
second->next=f2;
first=f2;
second=s2;
}
return head;
}
};/** Node shape: { val, next }. Build/return plain objects or use this ctor. */ function ListNode(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
/**
* @param {ListNode} head
* @return {ListNode}
*/ var reorderList = function(head) {
if(!head||!head.next)return head;
let slow=head,fast=head.next;
while(fast&&fast.next) {
slow=slow.next;
fast=fast.next.next;
}
let second=slow.next;
slow.next=null;
let prev=null;
while(second) {
const nxt=second.next;
second.next=prev;
prev=second;
second=nxt;
}
let first=head;
second=prev;
while(second) {
const f2=first.next,s2=second.next;
first.next=second;
second.next=f2;
first=f2;
second=s2;
}
return head;
};