mediumLinked ListPattern: Mixed

Alternate Node Reordering Solution

Problem Statement

Given a singly linked list, reorder its nodes such that all nodes at even indices appear before nodes at odd indices. The relative order of nodes within the even and odd groups must be preserved.

Examples

Example 1:
Input:1 -> 2 -> 3 -> 4 -> 5
Output:2 -> 4 -> 1 -> 3 -> 5
Explanation: Even-indexed nodes (2, 4) appear before odd-indexed nodes (1, 3, 5)
Example 2:
Input:1 -> 2 -> 3
Output:2 -> 1 -> 3
Explanation: Even-indexed node (2) appears before odd-indexed nodes (1, 3)

Constraints

  • You may not modify the original linked list
  • You must return the reordered linked list
Time: O(n) Space: O(1)
Use two separate lists to store even-indexed and odd-indexed nodes with a time complexity of O(n) and space complexity of O(n)

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.