#328
Medium Algorithms Odd even linked list
Linked List
62.3% acceptance
Jan 12, 2026
11408
592
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn odd_even_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() {
return None;
}
let mut head = head;
let mut odd = head.as_mut().unwrap();
let mut even_head = odd.next.take();
let mut even = even_head.as_mut();
while even.is_some() && even.as_ref().unwrap().next.is_some() {
let even_node = even.unwrap();
odd.next = even_node.next.take();
odd = odd.next.as_mut().unwrap();
even_node.next = odd.next.take();
even = even_node.next.as_mut();
}
odd.next = even_head;
head
}
}