Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Odd Even Linked List

Medium
linked-list

Group all nodes at odd positions (1st, 3rd, …) followed by nodes at even positions, preserving relative order within each group. Solve in O(1) space and O(n) time.

Input: one line of space-separated integers. Output: the regrouped list's values.

Example 1
Input:
1 2 3 4 5
Output:
1 3 5 2 4
Example 2
Input:
2 1 3 5 6 4 7
Output:
2 3 6 7 1 5 4

Constraints

  • 0 ≤ list length ≤ 10^4
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.