Losing head/tail while rewiring
Wrong move: Pointer updates overwrite references before they are saved.
Usually fails on: List becomes disconnected mid-operation.
Fix: Store next pointers first and use a dummy head for safer joins.
Move from brute-force thinking to an efficient approach using linked list strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Linked List
[1,2,3,4,5]
[2,1,3,5,6,4,7]
split-linked-list-in-parts)transform-array-by-parity)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #328: Odd Even Linked List
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null) {
return null;
}
ListNode a = head;
ListNode b = head.next, c = b;
while (b != null && b.next != null) {
a.next = b.next;
a = a.next;
b.next = a.next;
b = b.next;
}
a.next = c;
return head;
}
}
// Accepted solution for LeetCode #328: Odd Even Linked List
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func oddEvenList(head *ListNode) *ListNode {
if head == nil {
return nil
}
a := head
b, c := head.Next, head.Next
for b != nil && b.Next != nil {
a.Next = b.Next
a = a.Next
b.Next = a.Next
b = b.Next
}
a.Next = c
return head
}
# Accepted solution for LeetCode #328: Odd Even Linked List
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return None
a = head
b = c = head.next
while b and b.next:
a.next = b.next
a = a.next
b.next = a.next
b = b.next
a.next = c
return head
// Accepted solution for LeetCode #328: Odd Even Linked List
struct Solution;
use rustgym_util::*;
impl Solution {
fn odd_even_list(mut head: ListLink) -> ListLink {
let mut odd: Vec<ListLink> = vec![];
let mut even: Vec<ListLink> = vec![];
let mut i = 1;
while let Some(mut node) = head {
head = node.next.take();
if i % 2 == 1 {
odd.push(Some(node));
} else {
even.push(Some(node));
}
i += 1;
}
let mut res: ListLink = None;
while let Some(mut link) = even.pop() {
link.as_mut().unwrap().next = res;
res = link;
}
while let Some(mut link) = odd.pop() {
link.as_mut().unwrap().next = res;
res = link;
}
res
}
}
#[test]
fn test() {
let head = list![1, 2, 3, 4, 5];
let res = list![1, 3, 5, 2, 4];
assert_eq!(Solution::odd_even_list(head), res);
let head = list![2, 1, 3, 5, 6, 4, 7];
let res = list![2, 3, 6, 7, 1, 5, 4];
assert_eq!(Solution::odd_even_list(head), res);
}
// Accepted solution for LeetCode #328: Odd Even Linked List
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function oddEvenList(head: ListNode | null): ListNode | null {
if (!head) {
return null;
}
let [a, b, c] = [head, head.next, head.next];
while (b && b.next) {
a.next = b.next;
a = a.next;
b.next = a.next;
b = b.next;
}
a.next = c;
return head;
}
Use this to step through a reusable interview workflow for this problem.
Copy all n nodes into an array (O(n) time and space), then use array indexing for random access. Operations like reversal or middle-finding become trivial with indices, but the O(n) extra space defeats the purpose of using a linked list.
Most linked list operations traverse the list once (O(n)) and re-wire pointers in-place (O(1) extra space). The brute force often copies nodes to an array to enable random access, costing O(n) space. In-place pointer manipulation eliminates that.
Review these before coding to avoid predictable interview regressions.
Wrong move: Pointer updates overwrite references before they are saved.
Usually fails on: List becomes disconnected mid-operation.
Fix: Store next pointers first and use a dummy head for safer joins.