Group Odd-Position Then Even-Position Nodes of a Linked Feed

Last updated: November 29, 2026

Quick Overview

Given the head of a singly linked list, relink it so all nodes at odd positions (1st, 3rd, ...) come first, followed by all nodes at even positions, keeping relative order in each group, in O(n) time and O(1) space.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given the head of a singly linked list, relink it so all nodes at odd positions (1st, 3rd, ...) come first, followed by all nodes at even positions, keeping relative order in each group, in O(n) time and O(1) space.

A Meta phone-screen Easy framed as splitting a feed into two alternating lanes for an A/B display layout. Interviewers check that you can weave two pointers through the list without losing the even chain's head.

What the Interviewer Expects
  • Clarify positions are 1-indexed and about position, not value
  • Maintain odd and even tails moving in lockstep
  • Save the head of the even chain
  • Attach the even chain after the odd tail
  • Use O(1) extra space
Key Topics to Cover
Linked list
Two pointers
In-place relinking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Group by value parity instead of position.
  • Split into k lanes by position modulo k.
  • Undo the operation: interleave the two halves back.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: head of a list with n >= 0 nodes. Output: head of the relinked list: positions 1, 3, 5, ... in order, then positions 2, 4, 6, ... in order. No new nodes, no array copies....

High-Level Design

Keep odd at the first node and even at the second, and remember even_head. In each step, link odd.next = even.next and advance odd; then link even.next = odd.next and advance even. The loop ...


Submit Your Answer
Markdown supported