Home
DSA Patterns
Linked List
Reorder List
Reorder List (LeetCode 143)
Reorder List (LeetCode 143): reorder a linked list in a specific pattern.
Problem Understanding
Take a singly linked list L0 → L1 → … → Ln - 1 → Ln and rearrange its nodes so they alternate between the front and the back, changing only the links and never the values:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
Example: 1->2->3->4 becomes 1->4->2->3.
Attempt 1: Array/Deque (Brute Force)
We can put all nodes into a Deque (Double Ended Queue). Then, we can pop from the front and back alternately to build the new list.
- Drawback: Uses O(N) extra space to store all node references.
Visualizing the Issue
We need access to the end of the list, but we only have a forward pointer. Does this remind you of Palindrome check? We needed to access the end there too.
The Intuition: Split, Reverse, Merge
- Find Middle: Split the list into two halves.
- Reverse Second Half: The second half starts at
slow.next. For1->2->3->4,slowstops at3, so the halves are1->2->3and4; for1->2->3->4->5they are1->2->3and4->5, reversed to5->4. - Merge: Pick one from first, then one from second.
1 -> 4 -> 2 -> 3.
Reorder List Visualization
5 NODES · L0 → Ln → L1 → …
START AT HEAD
slow fast
#0
→
#1
→
#2
→
#3
→
#4
→ NULL
Phase
middle
slow and fast start at head, 1
Next
slow moves to 2; fast moves two to 3
Values
Legend & Complexity
slow, curr or first: the pointer driving this phase
fast, prev or second
The second half, once cut off
The reordered list
→ n: a link into the other row
Time
O(n)
Space
O(1)
Watch the three steps as structure: the cut at slow.next = NULL turns one row into two, flipping the second half makes that row read backward, and each splice links a node from the lower row into the upper one until a single row remains.
Reorder List Dry Run
Input: 1 -> 2 -> 3 -> 4
| Step | Action | State |
|---|---|---|
| 1 | Find Mid | fast moves 1 → 3 → NULL; slow stops at 3. |
| 2 | Split | The second half starts at slow.next (4); slow.next = NULL leaves 1->2->3 and 4. |
| 3 | Reverse 2nd | 4 on its own is already reversed. |
| 4 | Merge | Splice 4 after 1: 1->4->2->3. second is now NULL. |
| Result | - | 1 -> 4 -> 2 -> 3 |
For 1 -> 2 -> 3 -> 4 -> 5 the halves are 1->2->3 and 4->5, reversed to 5->4, and the merge gives 1 -> 5 -> 2 -> 4 -> 3.
Reorder List LeetCode Solution
Three-step process.
Reorder List Code
1var reorderList = function(head) {2 if (!head) return;3 let slow = head, fast = head;4 while (fast && fast.next) {5 slow = slow.next;6 fast = fast.next.next;7 }8 let prev = null, curr = slow.next;9 slow.next = null;10 while (curr) {11 let temp = curr.next;12 curr.next = prev;13 prev = curr;14 curr = temp;15 }16 let first = head, second = prev;17 while (second) {18 let tmp1 = first.next;19 let tmp2 = second.next;20 first.next = second;21 second.next = tmp1;22 first = tmp1;23 second = tmp2;24 }25};
Reorder List in Python, Java, C++, TypeScript and C
Reorder List in Python
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
if not head: return
# 1. Find Middle
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# 2. Reverse Second Half
prev, curr = None, slow.next
slow.next = None
while curr:
temp = curr.next
curr.next = prev
prev = curr
curr = temp
# 3. Merge
first, second = head, prev
while second:
tmp1, tmp2 = first.next, second.next
first.next = second
second.next = tmp1
first, second = tmp1, tmp2Reorder List in Java
class Solution {
public void reorderList(ListNode head) {
if (head == null) return;
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode prev = null, curr = slow.next;
slow.next = null;
while (curr != null) {
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
ListNode first = head, second = prev;
while (second != null) {
ListNode tmp1 = first.next;
ListNode tmp2 = second.next;
first.next = second;
second.next = tmp1;
first = tmp1;
second = tmp2;
}
}
}Reorder List in C++
class Solution {
public:
void reorderList(ListNode* head) {
if (!head) return;
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
ListNode *prev = nullptr, *curr = slow->next;
slow->next = nullptr;
while (curr) {
ListNode *temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
ListNode *first = head, *second = prev;
while (second) {
ListNode *tmp1 = first->next;
ListNode *tmp2 = second->next;
first->next = second;
second->next = tmp1;
first = tmp1;
second = tmp2;
}
}
};Reorder List in TypeScript
function reorderList(head: ListNode | null): void {
if (!head) return;
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow.next!;
fast = fast.next.next!;
}
let prev: ListNode | null = null, curr = slow.next;
slow.next = null;
while (curr) {
const temp: ListNode | null = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
let first: ListNode | null = head, second = prev;
while (second) {
const tmp1: ListNode | null = first!.next;
const tmp2: ListNode | null = second.next;
first!.next = second;
second.next = tmp1;
first = tmp1;
second = tmp2;
}
}Reorder List in C
void reorderList(struct ListNode* head) {
if (!head) return;
struct ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
struct ListNode *prev = NULL, *curr = slow->next;
slow->next = NULL;
while (curr) {
struct ListNode *temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
struct ListNode *first = head, *second = prev;
while (second) {
struct ListNode *tmp1 = first->next;
struct ListNode *tmp2 = second->next;
first->next = second;
second->next = tmp1;
first = tmp1;
second = tmp2;
}
}Edge Cases & Common Mistakes
- Odd vs Even Length: Split logic must handle both.
- Single Node: Should do nothing.
- Null check: Return if head is null.
Summary
This problem combines three fundamental Linked List operations into one: Finding Middle, Reversing, and Merging.
More practice: DSA interview questions with answers · patterns cheat sheet
