Odd Even Linked List solution explained

To solve Odd Even Linked List, walk the list with two pointers, one on the odd-position chain and one on the even-position chain, linking each to the node two steps ahead, then attach the saved head of the even chain after the last odd node. Positions count from 1 at the head, and the node values do not matter. The Odd Even Linked List solution runs in O(n) time with O(1) extra space and keeps both groups in their original order.

By Published 6 min read

The task, restated

Given the head of a singly linked list, rearrange it so that the first, third, fifth and every other odd-numbered node come first, followed by the second, fourth and remaining even-numbered nodes. Inside each group, nodes keep the order they had. Do it by changing pointers only, using constant extra memory, and return the head, which stays the same node.

Lists regrouped for this page
BeforeAfterNote
8 → 3 → 6 → 1 → 98 → 6 → 9 → 3 → 1Odd positions hold 8, 6, 9; even positions hold 3, 1
2 → 4 → 1 → 32 → 1 → 4 → 3Grouping by value would give a different, wrong answer
5 → 75 → 7One odd node, one even node: already in place
1111A single node
emptyemptyReturn null
Lists regrouped for this page. Scroll sideways on a small screen.

The second row is the one to say out loud early. Several candidates read "odd" as odd values and sort the list into odd and even numbers. Confirm with the interviewer that it is about positions, and whether the first node counts as position 1 (it usually does).

How interviewers use it

This question tests whether you can keep two chains growing at once without breaking either. The loop body is four lines, and the order of those lines matters. It also checks whether you think about the loop condition carefully, because odd and even list lengths end the walk at different places.

  • Group by value instead. Then it becomes a stable partition: build two chains with dummy heads based on node.val % 2.
  • Split into k groups by position mod k. Keep k tails and k heads, then join them in order.
  • Return two separate lists. Skip the final join and return both heads.
  • Interleave two lists. The reverse operation, which shows up in problems like reordering a list.

Walk the list with a position counter. Put nodes at odd positions in one array and nodes at even positions in another. Concatenate the arrays and set each node's next to the following node, with the last pointing to null. This is correct and easy to verify, but it allocates O(n) extra memory, which the problem rules out.

def odd_even_with_lists(head):
    odd, even = [], []
    pos, node = 1, head
    while node:
        (odd if pos % 2 else even).append(node)
        node = node.next
        pos += 1
    order = odd + even
    for a, b in zip(order, order[1:]):
        a.next = b
    if order:
        order[-1].next = None
    return order[0] if order else None
Python baseline: collect by position, then rebuild the links.

In place: two chains that leapfrog

Let odd start at the head and even at the second node, and remember that second node as even_head. On each round, the odd node links to the node after even (the next odd one) and moves there; then the even node links to the node after the new odd (the next even one) and moves there. When even runs out, or even is the last node, the odd chain is complete, and its last node points to even_head.

def odd_even_list(head):
    if head is None:
        return None
    odd, even = head, 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
Python: O(1) extra space; the head never changes.
function oddEvenList(head) {
  if (!head) return null;
  let odd = head, even = head.next;
  const evenHead = even;
  while (even && even.next) {
    odd.next = even.next;
    odd = odd.next;
    even.next = odd.next;
    even = even.next;
  }
  odd.next = evenHead;
  return head;
}
JavaScript: the same four-line loop body.

Why the loop condition is even and even.next: for an even-length list, even ends on the last node, and its next is already null, so the even chain is properly terminated. For an odd-length list, the last round sets even.next to null (the odd node it would point to has no successor) and even becomes null. Either way the even chain ends cleanly, and the final odd node is the one that gets joined to even_head.

Tracing 8 → 3 → 6 → 1 → 9

Pointer positions after each loop round
Roundodd ateven atOdd chainEven chain
Start8383
1618 → 63 → 1
29null8 → 6 → 93 → 1
Join98 → 6 → 9 → 3 → 1
Pointer positions after each loop round. Scroll sideways on a small screen.

Time and space

Complexity for a list of n nodes
ApproachTimeExtra space
Two arrays and re-linkO(n)O(n)
Two leapfrogging chainsO(n), each node visited onceO(1)
Complexity for a list of n nodes. Scroll sideways on a small screen.

Edge cases

  • Empty list and a single node: return as is.
  • Two nodes: the loop does not run, and odd.next is set back to the second node, which it already was.
  • Odd length versus even length: test both, since they exit the loop differently.
  • Duplicate values: harmless, because only positions matter.

Mistakes to avoid

  • Grouping by node value instead of position.
  • Not saving the head of the even chain before the loop rewires the links around the second node.
  • Moving odd forward before linking it, which skips a node.
  • Using while odd.next as the condition, which can step onto null and crash in the even update.
  • Allocating new nodes; the expected answer rearranges the existing ones.

How to explain it out loud

Fictional example

Explaining the loop before writing it
Candidate
Odd here means position, so the head stays first. I will keep two pointers that each jump two nodes, one building the odd chain and one the even chain.
Candidate
I save the second node as the even head, because once the loop runs I lose the easy way back to it.
Candidate
The loop continues while even and even.next exist. When it stops, the odd chain's last node points to the saved even head. Each node is touched once, constant extra memory.

Palindrome Linked List also splits a list into two halves in place, then compares them. Rotate List is the gentler warm-up for re-linking without leaving a cycle. Sort List uses the same split-and-join moves inside merge sort.

Where Natively fits

For practice on a Mac or Windows computer, Natively can read a linked list prompt from a screenshot and write out an approach and code, so you can compare its loop condition with yours after you have traced it by hand. A transcript of your mock session helps you hear whether you clarified "position versus value" before coding. The AI interview assistant page explains the setup.

Questions people ask

How do you solve Odd Even Linked List?

Keep one pointer on the odd-position chain and one on the even-position chain. Each links to the node two steps ahead, and at the end the last odd node points to the saved head of the even chain.

Does odd mean odd values or odd positions?

Positions. The head is position 1, so it stays first; the values inside the nodes do not matter. Grouping by value is a different problem.

What is the time complexity of Odd Even Linked List?

O(n) time, since each node is visited once, and O(1) extra space, because only a few pointers are used.

Why save the head of the even chain?

The loop rewires the links around the second node, so without a saved reference you could not attach the even chain after the last odd node.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP