Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get the head of a linked list L0 → L1 → L2 → … → Ln. Rearrange it into L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …: first node, last node, second node, second-to-last, and so on until they meet in the middle.
You must relink the nodes, not change the values inside them. The function returns nothing; it changes the list in place. The list has between 1 and 50,000 nodes.
Examples:
1 → 2 → 3 → 4→1 → 4 → 2 → 31 → 2 → 3 → 4 → 5→1 → 5 → 2 → 4 → 3
Start with brute force
The pain is that a singly linked list can't walk backwards, and you keep needing "the last node". So copy every node into an array. Now nodes[i] and nodes[j] give you any node instantly. Use two indices i = 0 and j = n − 1, link nodes[i] → nodes[j] → nodes[i + 1], move both inwards, and set the final node's next to null.
That's O(n) time, and honestly fine for an interview. But it uses O(n) extra space, and the follow-up will be "do it in O(1) space".
Doing it with no array at all, by walking to the current last node each time, is O(n²): 50,000 nodes means over a billion steps.
Building the intuition
Write out a bigger answer and stare at it:
input: 1 → 2 → 3 → 4 → 5 → 6
answer: 1 → 6 → 2 → 5 → 3 → 4Split the answer into the nodes at odd positions and the nodes at even positions:
- odd positions:
1, 2, 3. That's the first half, in order. - even positions:
6, 5, 4. That's the second half, backwards.
So the answer is just two lists zipped together: the first half, and the reversed second half. And each piece is something you already know how to do:
- Find the middle. Fast and slow pointers: when
fastreaches the end,slowis in the middle. - Reverse the second half. The same three moves as Reverse Linked List: save, flip, step.
- Zip the halves. Like Merge Two Sorted Lists, but you alternate instead of comparing.
This is why the problem shows up in interviews. It's three small tools glued together, and the interviewer wants to see you break it down.
The optimal approach
- Find the end of the first half. Start
slowandfastathead. Whilefast.nextandfast.next.nextexist, moveslowone step andfasttwo.slowstops on the last node of the first half. - Cut and reverse. Set
second = slow.next, thenslow.next = nullso the first half ends cleanly. Reverse the list starting atsecond. - Zip. With
first = headandsecond= head of the reversed half, whilesecondexists: save bothnextnodes, linkfirst → second → (old first.next), then move both pointers to their saved nexts.
The subtle detail is where the middle lands. With this loop condition, the first half gets the extra node when the length is odd (1 2 3 | 4 5), and the halves are equal when it's even (1 2 | 3 4). So the second half is never longer, and looping "while second exists" is enough. The middle node of an odd list ends up last, which is exactly where it belongs.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if head is None:
return
# 1. Find the end of the first half. Stopping when fast can't take two more
# steps leaves slow on the last node of the first (equal or longer) half.
slow = fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
# 2. Cut the list in two, then reverse the second half (save, flip, step).
second = slow.next
slow.next = None
prev = None
while second:
nxt = second.next
second.next = prev
prev = second
second = nxt
second = prev # head of the reversed second half: the old tail
# 3. Zip the halves: one node from the front, one from the (reversed) back.
first = head
while second: # the second half is never longer, so it runs out first
first_next, second_next = first.next, second.next # save both rests
first.next = second
second.next = first_next
first, second = first_next, second_nextclass Solution {
public:
void reorderList(ListNode* head) {
if (head == nullptr) return;
// 1. Find the end of the first half. Stopping when fast can't take two more
// steps leaves slow on the last node of the first (equal or longer) half.
ListNode* slow = head;
ListNode* fast = head;
while (fast->next != nullptr && fast->next->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
// 2. Cut the list in two, then reverse the second half (save, flip, step).
ListNode* second = slow->next;
slow->next = nullptr;
ListNode* prev = nullptr;
while (second != nullptr) {
ListNode* nxt = second->next;
second->next = prev;
prev = second;
second = nxt;
}
second = prev; // head of the reversed second half: the old tail
// 3. Zip the halves: one node from the front, one from the (reversed) back.
ListNode* first = head;
while (second != nullptr) { // the second half is never longer, so it runs out first
ListNode* firstNext = first->next; // save both rests
ListNode* secondNext = second->next;
first->next = second;
second->next = firstNext;
first = firstNext;
second = secondNext;
}
}
};class Solution {
public void reorderList(ListNode head) {
if (head == null) return;
// 1. Find the end of the first half. Stopping when fast can't take two more
// steps leaves slow on the last node of the first (equal or longer) half.
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// 2. Cut the list in two, then reverse the second half (save, flip, step).
ListNode second = slow.next;
slow.next = null;
ListNode prev = null;
while (second != null) {
ListNode nxt = second.next;
second.next = prev;
prev = second;
second = nxt;
}
second = prev; // head of the reversed second half: the old tail
// 3. Zip the halves: one node from the front, one from the (reversed) back.
ListNode first = head;
while (second != null) { // the second half is never longer, so it runs out first
ListNode firstNext = first.next; // save both rests
ListNode secondNext = second.next;
first.next = second;
second.next = firstNext;
first = firstNext;
second = secondNext;
}
}
}/**
* @param {ListNode} head
* @return {void} Do not return anything, modify head in-place instead.
*/
var reorderList = function (head) {
if (head === null) return;
// 1. Find the end of the first half. Stopping when fast can't take two more
// steps leaves slow on the last node of the first (equal or longer) half.
let slow = head;
let fast = head;
while (fast.next !== null && fast.next.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
// 2. Cut the list in two, then reverse the second half (save, flip, step).
let second = slow.next;
slow.next = null;
let prev = null;
while (second !== null) {
const nxt = second.next;
second.next = prev;
prev = second;
second = nxt;
}
second = prev; // head of the reversed second half: the old tail
// 3. Zip the halves: one node from the front, one from the (reversed) back.
let first = head;
while (second !== null) { // the second half is never longer, so it runs out first
const firstNext = first.next; // save both rests
const secondNext = second.next;
first.next = second;
second.next = firstNext;
first = firstNext;
second = secondNext;
}
};Dry run
head = 1 → 2 → 3 → 4 → 5.
Step 1, find the middle:
slow | fast | Can fast take 2 more steps? |
|---|---|---|
| 1 | 1 | yes |
| 2 | 3 | yes |
| 3 | 5 | no, stop |
Step 2, cut after 3 and reverse: first half 1 → 2 → 3, second half 4 → 5 becomes 5 → 4.
Step 3, zip. "List from head" is what you'd see walking from head.
| Iteration | first | second | Links made | List from head | Left in second |
|---|---|---|---|---|---|
| 1 | 1 | 5 | 1 → 5, 5 → 2 | 1 → 5 → 2 → 3 | 4 |
| 2 | 2 | 4 | 2 → 4, 4 → 3 | 1 → 5 → 2 → 4 → 3 | empty |
second is null, so we stop. 3.next is already null from the cut, so the list ends correctly.
Complexity
- Time: O(n). Finding the middle is about n/2 steps, reversing is n/2, and zipping is n/2.
- Space: O(1). A handful of pointers. Nothing grows with the list.
Edge cases and traps
- Forgetting the cut (
slow.next = null). The first half still runs into the second half, and you build a cycle. Tests then time out instead of failing cleanly. - One or two nodes.
fast.next.nextdoesn't exist, soslowstays onhead. The second half has zero or one node, and the zip does nothing or one harmless link. - Saving only one
nextduring the zip. You change bothfirst.nextandsecond.next, so save both before you touch either. - Swapping values instead of nodes. The problem asks you to relink nodes. Changing
valfields is not allowed. - Returning the head. The function returns nothing (
void). Edit the list in place.
Say it in the interview
"If I look at the answer, the odd positions are the first half in order and the even positions are the second half reversed. So I'll do it in three steps. Fast and slow pointers to find the middle, then cut the list there. Reverse the second half in place. Then zip the two halves, taking one node from each, saving both nexts before I relink. Each step is O(n) and uses O(1) extra space, so overall O(n) time and O(1) space."
Follow-ups interviewers ask
- "Could you just use a stack or array?" Yes: push all nodes, then pair the front with the back. It's simpler but O(n) space, which is why the three-step version is the expected answer.
- "How would you test it?" Lengths 1, 2, 3, 4 and 5 cover every way the middle can land. Then walk the result and check that it ends in
null(no accidental cycle). - "Check if a list is a palindrome in O(1) space." Same first two steps: find the middle, reverse the second half, then compare the halves node by node instead of zipping.
Practise the same idea
This is Day 46 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free