Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in O(1) extra space complexity and O(n) time complexity.
Example 1:
Input: head = [1,2,3,4,5] Output: [1,3,5,2,4]
Example 2:
Input: head = [2,1,3,5,6,4,7] Output: [2,3,6,7,1,5,4]
Constraints:
[0, 104].-106 <= Node.val <= 106Problem Overview: Rearrange a singly linked list so that nodes at odd indices appear first, followed by nodes at even indices. The relative order inside the odd group and even group must stay the same. Indexing is based on node position in the list, not the node values.
Approach 1: Two Pointers (O(n) time, O(1) space)
The optimal solution splits the list into two chains while traversing it once. Maintain two pointers: odd for nodes at odd positions and even for nodes at even positions. Also keep a reference to the head of the even list (evenHead) so it can be attached after the odd sequence at the end. During iteration, update pointers by skipping alternating nodes: odd.next = even.next and even.next = odd.next. This effectively builds two sublists in-place without extra memory. When traversal finishes, connect the last odd node to evenHead. The list is rearranged in a single pass with constant extra space, which makes this the expected interview solution when working with linked list pointer manipulation.
The key insight is that nodes do not need to be reordered individually; instead, the existing next pointers are rewired to group odd and even positions. Since every node is visited once, the runtime stays linear. This pattern—maintaining two moving references while rewiring pointers—is common in problems involving two pointers on linked structures.
Approach 2: Recursive Re-linking (O(n) time, O(n) space)
A recursive approach processes the list in pairs and rebuilds the odd and even sequences during the recursion stack unwinding. At each recursive step, treat the current node as an odd node and its next node as an even node. Recursively process the rest of the list starting from head.next.next. The recursive call returns the reorganized list of remaining nodes, which is then attached to maintain the odd-first ordering.
This method expresses the problem more declaratively: each call handles a small segment and delegates the remainder of the list to recursion. However, recursion consumes stack space proportional to the number of nodes, leading to O(n) auxiliary space. Pointer-heavy linked list problems rarely benefit from recursion in production code, but it can help illustrate how the list structure evolves step by step. It also reinforces understanding of pointer references and structural decomposition commonly seen in recursion-based linked list problems.
Recommended for interviews: The two pointers approach is the expected answer. It runs in O(n) time and O(1) space while modifying the list in-place. Interviewers look for correct pointer updates and careful handling of the even list head. The recursive approach demonstrates conceptual understanding but is rarely preferred because of the additional stack usage.
This approach uses two pointers to manage odd and even indexed nodes separately. We maintain separate pointers for the head of the odd and even lists and iterate through the given list. As we go along, we link odd indices to the next odd and even indices to the next even. In the end, we connect the last odd indexed node to the head of the even indexed list.
The code begins by checking if the list is empty or just a single node, in which case it simply returns the list as is. Two pointers, 'odd' and 'even', are initialized to the first and second nodes, respectively. The 'evenHead' pointer is used to remember the start of the even index list. We then iterate through the list, linking odd nodes to the next odd nodes and even nodes to the next even nodes. Finally, odd nodes are linked to the 'evenHead'.
Time Complexity is O(n) since we traverse the entire list once. Space Complexity is O(1) because we're rearranging pointers without using any extra space for storage.
Although not typical due to O(1) space requirement, this problem could conceptually be solved recursively by defining a function that processes the head and its next elements recursively, managing pointers for odd and even segments. As recursion inherently uses extra space on the stack, this isn't space-optimal, thus academically interesting but not compliable with constant space constraints.
This Python solution uses a local function 'partition' which recursively treats nodes as alternating odd or even. Despite functional recursion achieving the same odd-even connection, it ruins constant space by occupying stack memory, which might exceed constraints under deep recursion.
Python
Time Complexity remains O(n) for full list traversal. However, Space Complexity rises to O(n) because of the call stack in recursion, unsuitable under given problem constraints for constant space.
We can use two pointers a and b to represent the tail nodes of the odd and even nodes respectively. Initially, pointer a points to the head node head of the list, and pointer b points to the second node head.next of the list. In addition, we use a pointer c to point to the head node head.next of the even nodes, which is the initial position of pointer b.
We traverse the list, set pointer a to point to the next node of b, i.e., a.next = b.next, then move pointer a back by one position, i.e., a = a.next; set pointer b to point to the next node of a, i.e., b.next = a.next, then move pointer b back by one position, i.e., b = b.next. Continue to traverse until b reaches the end of the list.
Finally, we set the tail node a of the odd nodes to point to the head node c of the even nodes, i.e., a.next = c, then return the head node head of the list.
The time complexity is O(n), where n is the length of the list, and we need to traverse the list once. The space complexity is O(1). We only need to maintain a limited number of pointers.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Two Pointers Approach | Time Complexity is O(n) since we traverse the entire list once. Space Complexity is O(1) because we're rearranging pointers without using any extra space for storage. |
| Recursive Approach | Time Complexity remains O(n) for full list traversal. However, Space Complexity rises to O(n) because of the call stack in recursion, unsuitable under given problem constraints for constant space. |
| Single Pass | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two Pointers | O(n) | O(1) | Best general solution. Rearranges nodes in-place with one traversal. |
| Recursive Re-linking | O(n) | O(n) | Useful for conceptual understanding of linked list restructuring using recursion. |
L6. Odd Even Linked List | Multiple Approaches • take U forward • 250,617 views views
Watch 9 more video solutions →Practice Odd Even Linked List with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor