Try the Problem
You can practice the problem here:
https://leetcode.com/problems/odd-even-linked-list/
Problem Description (In Very Simple Words)
You are given the head of a linked list.
Your task is:
👉 Rearrange the list such that:
- All nodes at odd positions come first
- Followed by all nodes at even positions
Important Clarification
❗ This problem is about positions (indices), NOT values.
- 1st node → Odd
- 2nd node → Even
- 3rd node → Odd
- 4th node → Even
Example Walkthrough
Example 1
Input:
Positions:
Output:
Example 2
Input:
Output:
Constraints
Core Idea of the Problem
We need to:
- Separate nodes into two groups:
- Odd index nodes
- Even index nodes
- Maintain their original order
- Finally:
- 👉 Attach even list after odd list
Thinking About Different Approaches
When solving this problem, multiple approaches may come to mind:
Approach 1: Create New Lists
Idea
- Traverse the list
- Create new nodes for odd and even positions
- Build two separate lists
- Merge them
Problem with This Approach
❌ Uses extra space
❌ Not optimal (violates O(1) space requirement)
❌ Code becomes messy and harder to maintain
Your approach is logically correct, but:
👉 It is not optimal and can be simplified a lot.
Approach 2: Brute Force Using Array/List
Idea
- Store nodes in an array
- Rearrange based on indices
- Rebuild linked list
Complexity
- Time:
O(n) - Space:
O(n)❌ (Not allowed)
Approach 3: Optimal In-Place Approach (Best Solution)
This is the cleanest and most important approach.
Optimal Approach: Two Pointer Technique (In-Place)
Idea
Instead of creating new nodes:
👉 We rearrange the existing nodes
We maintain:
odd→ points to odd nodeseven→ points to even nodesevenHead→ stores start of even list
Visualization
Input:
We separate into:
Final:
Step-by-Step Logic
Step 1: Initialize pointers
Step 2: Traverse the list
While:
Update:
Move pointers forward:
Step 3: Merge
Clean Java Implementation (Optimal)
Dry Run (Important)
Input:
Steps:
Final connection:
Result:
Time Complexity
We traverse the list once.
Space Complexity
No extra space used.
Why This Approach is Best
| Feature | Result |
| Extra Space | ❌ None |
| Clean Code | ✅ Yes |
| Efficient | ✅ O(n) |
| Interview Friendly | ✅ Highly |
Common Mistakes
❌ Confusing values with positions
❌ Creating new nodes unnecessarily
❌ Forgetting to connect even list at the end
❌ Breaking the list accidentally
Key Learning
This problem teaches:
- In-place linked list manipulation
- Pointer handling
- List partitioning
Final Thoughts
The Odd Even Linked List problem is a classic example of how powerful pointer manipulation can be.
Even though creating new nodes might seem easier at first, the in-place approach is:
👉 Faster
👉 Cleaner
👉 Interview optimized
👉 Tip: Whenever you are asked to rearrange a linked list, always think:
"Can I solve this by just changing pointers instead of creating new nodes?"
That’s the key to mastering linked list problems 🚀




