Odd Even Linked List
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement Solution.oddEvenList(head). Reconnect the existing nodes so positions 1,3,5,... come before positions 2,4,6,..., preserving the order within each group. Return the head using O(1) extra space.
Starter code
class Solution:
def oddEvenList(self, head):
passTest cases
five-nodes
{
"args": [
{
"$type": "linked-list",
"values": [
1,
2,
3,
4,
5
]
}
]
}Expected: {"$type":"linked-list","values":[1,3,5,2,4]}
Wizard outline
- Step 1: Initialize Solution.oddEvenList
Replace the empty starter with the first real state owned by Solution.oddEvenList. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the Five Nodes case
Complete the readable core algorithm for one representative Interview case. Maintain odd and even tails and reconnect the odd tail to the saved even head.
- Step 4: Harden the Six Nodes boundary
Repair the reviewed boundary and pass the complete submission contract. Each loop appends the next odd-position node to the odd chain and the next even-position node to the even chain without changing order. After all pairs are processed, joining odd to the saved even head yields exactly the requested stable grouping.
Footguns and prerequisites
- Odd and even refer to one-based node positions, not the integer values stored in nodes.
- linked lists
Reviewed references
Practice prerequisites
- Relink Nodes Safely(opens in a new tab)
Relink Nodes Safely isolates every untouched node keeps its original next link, and at most one predecessor changes to the removed node’s former successor. That focused state discipline is required when implementing odd even linked list as a complete Interview Problem.
Recommended approach and implementation
Save the first even node, advance odd and even tails by skipping alternating nodes, then append the saved even chain after the odd chain.
Why it works: Each loop appends the next odd-position node to the odd chain and the next even-position node to the even chain without changing order. After all pairs are processed, joining odd to the saved even head yields exactly the requested stable grouping.
class Solution:
def oddEvenList(self, head):
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
if not head or not head.next:
return head
odd = head
even = head.next
even_head = even
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = even_head
return head