Odd Even Linked List
Master this topic with zero to advance depth.
Odd Even Linked List
Group nodes with odd indices together followed by even indices.
Visual Representation
1 -> 2 -> 3 -> 4 -> 5
Odd: 1 -> 3 -> 5
Even: 2 -> 4
Result: 1 -> 3 -> 5 -> 2 -> 4Examples
Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]
Approach 1
Level I: Brute Force (Two Lists/Arrays)
Intuition
Traverse the list and store odd-indexed nodes in one list/array and even-indexed nodes in another. Then, link the end of the odd list to the head of the even list.
⏱ O(N)💾 O(N)
Detailed Dry Run
1->2->3->4. Odd: [1, 3]. Even: [2, 4]. Result: 1->3->2->4.
Approach 2
Level III: Two Pointers (In-place)
Intuition
Maintain odd and even pointers, cross-linking as you traverse.
⏱ O(N)💾 O(1)
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.