BackmediumLinked List Amazon

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.

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
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free