PPractick

DSA interview question

Odd Even Linked List

Medium · Data structures · Topics: linkedlist

The problem

A printing queue interleaves jobs from two departments: positions 1, 3, 5, ... belong to Finance and positions 2, 4, 6, ... to Marketing. The manager wants all of Finance's jobs first, then all of Marketing's, each group keeping its original relative order, and the reshuffle must happen in place with O(1) extra memory. Two pointers do it by threading two chains, one through the odd-position nodes and one through the even-position nodes, and then joining the end of the odd chain to the start of the even chain.

Example: 1 -> 2 -> 3 -> 4 -> 5 → 1 -> 3 -> 5 -> 2 -> 4.

Function signature

Node* oddEvenList(Node* head);  // odd positions (1st, 3rd, ...) first, then even positions

Test cases

Input listOutput listExplanation
1 -> 2 -> 3 -> 4 -> 51 -> 3 -> 5 -> 2 -> 4
2 -> 1 -> 3 -> 5 -> 6 -> 4 -> 72 -> 3 -> 6 -> 7 -> 1 -> 5 -> 4Positions, not values, decide the group
1 -> 21 -> 2Already grouped
11
(empty)(empty)

(Confirm: "odd" and "even" refer to 1-indexed positions, not values (LeetCode 328); relative order within each group is preserved.)

How interviewers phrase it

Clarifying questions to ask

  1. Odd and even by position or by value?
  2. Is the first node position 1 (odd)?
  3. Must it be O(1) extra space?

Follow-ups to expect

  1. "Why keep a pointer to the first even node?"
  2. "What's the loop condition?"
  3. "How would you segregate by value instead?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in