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 <= 106When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a line of people holding hands, where everyone has a numbered position from first to last. The brute force approach involves completely taking the line apart, making separate piles for the people in odd positions and the people in even positions, and then gluing them back together.
Here's how the algorithm would work step-by-step:
def odd_even_list(head):
if not head or not head.next:
return head
odd_nodes_list = []
even_nodes_list = []
current_node = head
is_odd_position = True
while current_node:
if is_odd_position:
odd_nodes_list.append(current_node)
else:
even_nodes_list.append(current_node)
current_node = current_node.next
is_odd_position = not is_odd_position
# We collect all nodes into separate lists to decouple them from their original interleaved positions
combined_nodes = odd_nodes_list + even_nodes_list
for index in range(len(combined_nodes) - 1):
combined_nodes[index].next = combined_nodes[index + 1]
# We rebuild the pointer connections so that every node correctly links to its newly assigned successor
combined_nodes[-1].next = None
return combined_nodes[0]The optimal approach rearranges the list by separating the items into two distinct groups based on their original positions, keeping their relative order intact. Finally, it connects the end of the first group directly to the beginning of the second group to form a single, seamlessly merged chain.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def odd_even_list(head_node):
if not head_node or not head_node.next:
return head_node
odd_group_head = head_node
even_group_head = head_node.next
current_odd_node = odd_group_head
current_even_node = even_group_head
# We track the even head because we need it later to bridge the two groups together.
even_list_start = even_group_head
# We traverse while there are even nodes and subsequent nodes to weave together.
while current_even_node and current_even_node.next:
current_odd_node.next = current_even_node.next
current_odd_node = current_odd_node.next
current_even_node.next = current_odd_node.next
current_even_node = current_even_node.next
# We connect the tail of the odd chain directly to the preserved head of the even chain.
current_odd_node.next = even_list_start
return odd_group_head| Case | How to Handle |
|---|---|
| Null head or single node linked list | The algorithm should immediately return the head since no reordering is necessary for lists with zero or one element. |
| Linked list with exactly two nodes | The odd pointer stays at the first node, the even pointer stays at the second, and linking is trivial without changes. |
| Linked list with an odd total number of nodes | The even pointer will naturally reach the end and become null while the odd pointer handles the final node correctly. |
| Linked list with an even total number of nodes | The odd pointer will reach the second-to-last node before the even pointer hits the end, allowing clean termination. |
| Very long linked lists scaling to maximum constraints | An iterative two-pointer approach ensures O(1) auxiliary space complexity and O(n) time complexity without stack overflow risks. |
| Nodes containing negative numbers, zeros, or duplicates | Since the grouping is purely index-based rather than value-based, node values do not affect the pointer rearrangement logic. |