Reorder List (LeetCode 143): Solution and Explanation
You get the head of a singly linked list and must rearrange its nodes so the order alternates between the front and the back: first node, last node, second node, second to last, and so on. The change happens in place by relinking nodes, not by swapping values or returning a new list.
Watch it run step by step
Try your own input and write the code →Intuition
The simplest idea is to copy every node into an array. With random access you can walk two indices toward each other, one from the front and one from the back, and relink the nodes in the alternating order. That works in O(n) time but uses O(n) extra memory, and the whole point of a linked list question like this is usually to see whether you can avoid that.
Look at the target order more carefully. The nodes taken from the front appear in their original order: L0, L1, L2. The nodes taken from the back appear in reverse order: Ln, Ln-1, Ln-2. So the answer is really two lists woven together, the first half as it is and the second half reversed. Once you see that, the problem breaks into three standard linked list moves you already know.
First, find the middle with slow and fast pointers, so you can cut the list into two halves without counting its length. Second, reverse the second half in place with the usual three-pointer reversal. Third, merge the two halves by taking one node from each in turn. Every step is a single linear pass that only rearranges next pointers, which gives O(n) time and constant extra space.
Approach: Find middle, reverse second half, merge
- If the list is empty, return immediately because there is nothing to reorder.
- Start
slowatheadandfastathead.next, then advanceslowby one andfastby two whilefastandfast.nextexist. When the loop ends,slowsits at the last node of the first half. - Set
second = slow.nextand cut the list by settingslow.next = None, so the first half now ends cleanly. - Reverse the second half by walking it with
prevandtmp, pointing each node back at the previous one. When done,previs the head of the reversed half. - Merge by setting
first = headandsecond = prev. Savefirst.nextandsecond.next, linkfirsttosecondandsecondto the saved next offirst, then advance both pointers. - Stop when
secondruns out. The second half is never longer than the first, so the leftover node of an odd-length list is already in the right spot at the end.
Step-by-step example
head = [1,2,3,4,5]
- Start with
slowat 1 andfastat 2. After one move,slowis 2 andfastis 4. After another,slowis 3 andfastis None, so the loop stops. - The first half is 1 -> 2 -> 3. Cutting at
slowgives a second half of 4 -> 5. - Reversing the second half produces 5 -> 4, with
prevpointing at 5. - Merge round one: link 1 to 5 and 5 to 2. Now
firstis 2 andsecondis 4. - Merge round two: link 2 to 4 and 4 to 3. Now
secondis None, so the loop ends. - The list reads 1 -> 5 -> 2 -> 4 -> 3, and node 3 already ends the list because its
nextwas cleared during the cut.
Code: Python, Java, C++
1class Solution:
2 def reorderList(self, head: Optional[ListNode]) -> None:
3 if not head: return
4
5 # Find middle
6 slow, fast = head, head.next
7 while fast and fast.next:
8 slow = slow.next
9 fast = fast.next.next
10
11 # Reverse second half
12 second = slow.next
13 prev = slow.next = None
14 while second:
15 tmp = second.next
16 second.next = prev
17 prev = second
18 second = tmp
19
20 # Merge two halves
21 first, second = head, prev
22 while second:
23 tmp1, tmp2 = first.next, second.next
24 first.next = second
25 second.next = tmp1
26 first, second = tmp1, tmp21class Solution {
2 public void reorderList(ListNode head) {
3 if (head == null) return;
4
5 // Find middle
6 ListNode slow = head, fast = head.next;
7 while (fast != null && fast.next != null) {
8 slow = slow.next;
9 fast = fast.next.next;
10 }
11
12 // Reverse second half
13 ListNode second = slow.next;
14 slow.next = null;
15 ListNode prev = null;
16 while (second != null) {
17 ListNode tmp = second.next;
18 second.next = prev;
19 prev = second;
20 second = tmp;
21 }
22
23 // Merge
24 ListNode first = head;
25 second = prev;
26 while (second != null) {
27 ListNode tmp1 = first.next;
28 ListNode tmp2 = second.next;
29 first.next = second;
30 second.next = tmp1;
31 first = tmp1;
32 second = tmp2;
33 }
34 }
35}1class Solution {
2public:
3 void reorderList(ListNode* head) {
4 if (!head) return;
5
6 // Find middle
7 ListNode *slow = head, *fast = head->next;
8 while (fast && fast->next) {
9 slow = slow->next;
10 fast = fast->next->next;
11 }
12
13 // Reverse second half
14 ListNode *second = slow->next;
15 slow->next = nullptr;
16 ListNode *prev = nullptr;
17 while (second) {
18 ListNode *tmp = second->next;
19 second->next = prev;
20 prev = second;
21 second = tmp;
22 }
23
24 // Merge
25 ListNode *first = head;
26 second = prev;
27 while (second) {
28 ListNode *tmp1 = first->next;
29 ListNode *tmp2 = second->next;
30 first->next = second;
31 second->next = tmp1;
32 first = tmp1;
33 second = tmp2;
34 }
35 }
36};Time and space complexity
O(n)
Finding the middle visits about half the nodes, reversing touches the second half once, and merging touches every node once. Three linear passes add up to O(n).
O(1)
Only a handful of pointer variables are used. The nodes are relinked in place and no array, stack or recursion is involved.
Other approaches
Array of nodes with two indices (time O(n), space O(n))
Store every node in an array, then use a left index and a right index moving toward each other to relink the nodes in alternating order, finally setting the last node's next to None. It is easier to get right under pressure but costs linear memory.
Stack for the back half (time O(n), space O(n))
Push all nodes onto a stack, then walk from the head and pop from the stack to get nodes from the back, inserting each one after the current front node until the pointers meet. It avoids the explicit reversal but still uses O(n) space.
Common mistakes
- Forgetting to cut the list with
slow.next = Nonebefore reversing. Without the cut, the first half still points into the second half and the merge creates a cycle. - Starting
fastatheadinstead ofhead.nextchanges whereslowstops. On even-length lists that puts the extra node in the first half's wrong position unless you adjust the rest of the code to match. - Losing references during the merge. You have to save both
first.nextandsecond.nextbefore rewiring, or you lose the rest of one list. - Looping the merge on
firstinstead ofsecond. The first half can be one node longer, so the merge must stop when the reversed half is exhausted.
Frequently asked questions
Why does Reorder List need to reverse the second half?
The nodes from the back of the list must be visited from last to first, but a singly linked list only lets you move forward. Reversing the second half turns that backward walk into a forward one, so a simple two-pointer merge can produce the alternating order.
Can Reorder List be solved in O(1) space?
Yes. The three-step approach of finding the middle, reversing the second half and merging only rearranges pointers, so it uses constant extra memory. The array and stack approaches are simpler but cost O(n) space.
What happens with an odd number of nodes?
The first half keeps the middle node, so it is one node longer than the second half. The merge stops when the second half runs out, and the middle node naturally ends up last, which is exactly where it belongs.
Why is the function return type None?
The problem asks you to modify the list in place. The caller still holds the original head, which stays the first node, so there is nothing new to return.