328. Odd Even Linked List
Problem Description
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:
- The number of nodes in the linked list is in the range
[0, 10⁴] -10⁶ <= Node.val <= 10⁶
Solution
Python Solution
class Solution:
def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
# Initialize pointers
odd = head
even = head.next
even_head = even
# Process nodes in pairs
while even and even.next:
# Connect odd nodes
odd.next = even.next
odd = odd.next
# Connect even nodes
even.next = odd.next
even = even.next
# Connect odd tail to even head
odd.next = even_head
return head
Time Complexity: O(n)
One pass through the linked list.
Space Complexity: O(1)
Only using constant extra space.
Java Solution
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
// Initialize pointers
ListNode odd = head;
ListNode even = head.next;
ListNode evenHead = even;
// Process nodes in pairs
while (even != null && even.next != null) {
// Connect odd nodes
odd.next = even.next;
odd = odd.next;
// Connect even nodes
even.next = odd.next;
even = even.next;
}
// Connect odd tail to even head
odd.next = evenHead;
return head;
}
}
Time Complexity: O(n)
One pass through the linked list.
Space Complexity: O(1)
Only using constant extra space.
C++ Solution
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (!head || !head->next) {
return head;
}
// Initialize pointers
ListNode* odd = head;
ListNode* even = head->next;
ListNode* evenHead = even;
// Process nodes in pairs
while (even && even->next) {
// Connect odd nodes
odd->next = even->next;
odd = odd->next;
// Connect even nodes
even->next = odd->next;
even = even->next;
}
// Connect odd tail to even head
odd->next = evenHead;
return head;
}
};
Time Complexity: O(n)
One pass through the linked list.
Space Complexity: O(1)
Only using constant extra space.
JavaScript Solution
/**
* @param {ListNode} head
* @return {ListNode}
*/
var oddEvenList = function(head) {
if (!head || !head.next) {
return head;
}
// Initialize pointers
let odd = head;
let even = head.next;
const evenHead = even;
// Process nodes in pairs
while (even && even.next) {
// Connect odd nodes
odd.next = even.next;
odd = odd.next;
// Connect even nodes
even.next = odd.next;
even = even.next;
}
// Connect odd tail to even head
odd.next = evenHead;
return head;
};
Time Complexity: O(n)
One pass through the linked list.
Space Complexity: O(1)
Only using constant extra space.
C# Solution
public class Solution {
public ListNode OddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
// Initialize pointers
ListNode odd = head;
ListNode even = head.next;
ListNode evenHead = even;
// Process nodes in pairs
while (even != null && even.next != null) {
// Connect odd nodes
odd.next = even.next;
odd = odd.next;
// Connect even nodes
even.next = odd.next;
even = even.next;
}
// Connect odd tail to even head
odd.next = evenHead;
return head;
}
}
Time Complexity: O(n)
One pass through the linked list.
Space Complexity: O(1)
Only using constant extra space.
Approach Explanation
The solution uses two-pointer technique:
- Key Insight:
- Separate odd and even indices
- Maintain two lists
- Connect at the end
- In-place modification
- Algorithm Steps:
- Initialize pointers
- Process node pairs
- Connect odd nodes
- Connect even nodes
Example walkthrough:
- Input: 1->2->3->4->5
- Odd nodes: 1->3->5
- Even nodes: 2->4
- Result: 1->3->5->2->4
Optimization insights:
- No extra space needed
- Single pass solution
- Maintain order
- Handle null checks
Edge Cases:
- Empty list
- Single node
- Two nodes
- Odd length list