Reorder List
Medium · Linked lists
Jump to codeRearrange a singly linked list in place so its nodes alternate from the front and back: first, last, second, second-last, and so on. Keep node values unchanged.
Examples
Example 1
head = [1,2,3,4]
Output: [1,4,2,3]
Example 2
head = [1,2,3,4,5]
Output: [1,5,2,4,3]
Constraints
- 1 <= Length of the list <= 1000.
- 1 <= Node.val <= 1000
Function contract
Linked lists are displayed as value arrays. Your code receives ListNode objects with val and next fields.
Modify head in place. The grader checks its value after your function returns.
Need a hint?
Split, reverse, then weave.
Real linked list interview questions
Questions on the same topic that candidates reported from real company interviews. Some company questions are locked and open with Premium or XP.
- Flatten a Linked List with Nested Side Branches into One Doubly Linked ListTesla
- Find the Node Where Two Singly Linked Lists MergeLinkedIn
- Remove duplicates from a singly linked listSalesforce
Solution approach
Find the midpoint with slow and fast pointers. Disconnect the second half and reverse it. Alternately splice one node from each half, saving next pointers before rewiring. The first half may contain one extra node for odd lengths. Disconnecting the halves prevents cycles.
Complexity
O(n) time and O(1) extra space.
Python reference solution
def reorderList(head):
if not head or not head.next:
return
slow, fast = head, head.next
while fast and fast.next:
slow, fast = slow.next, fast.next.next
current, slow.next = slow.next, None
second = None
while current:
following = current.next
current.next = second
second, current = current, following
first = head
while second:
first_next, second_next = first.next, second.next
first.next, second.next = second, first_next
first, second = first_next, second_next
JavaScript solution
function reorderList(head) {
if (!head || !head.next) return;
let slow = head, fast = head.next;
while (fast && fast.next) {
slow = slow.next; fast = fast.next.next;
}
let current = slow.next, second = null;
slow.next = null;
// Reverse the second half, then weave the halves together.
while (current) {
const following = current.next;
current.next = second;
second = current; current = following;
}
let first = head;
while (second) {
const firstNext = first.next, secondNext = second.next;
first.next = second; second.next = firstNext;
first = firstNext; second = secondNext;
}
}
Java solution
import java.util.*;
class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next; fast = fast.next.next;
}
ListNode current = slow.next, second = null;
slow.next = null;
// Reverse the second half, then weave the halves together.
while (current != null) {
ListNode following = current.next;
current.next = second;
second = current; current = following;
}
ListNode first = head;
while (second != null) {
ListNode firstNext = first.next, secondNext = second.next;
first.next = second; second.next = firstNext;
first = firstNext; second = secondNext;
}
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void reorderList(ListNode* head) {
if (!head || !head->next) return;
ListNode *slow = head, *fast = head->next;
while (fast && fast->next) {
slow = slow->next; fast = fast->next->next;
}
ListNode *current = slow->next, *second = nullptr;
slow->next = nullptr;
// Reverse the second half, then weave the halves together.
while (current) {
ListNode* following = current->next;
current->next = second;
second = current; current = following;
}
ListNode* first = head;
while (second) {
ListNode *firstNext = first->next, *secondNext = second->next;
first->next = second; second->next = firstNext;
first = firstNext; second = secondNext;
}
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 6 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[
[
1,
2,
3,
4
]
][ 1, 4, 2, 3 ]
[
[
1,
2,
3,
4,
5
]
][ 1, 5, 2, 4, 3 ]
[
[
1
]
][ 1 ]
[
[
1,
2
]
][ 1, 2 ]
[
[
1,
2,
3
]
][ 1, 3, 2 ]
Drafts are saved per language in this browser when storage is available.