BackhardLinked List

Interleaved Reversal of Nodes in a Sequence Solution

Problem Statement

Given a sequence of interconnected nodes, rearrange the nodes such that the sequence alternates between nodes from the first half and nodes from the second half of the original sequence, effectively creating an interleaved reversal.

Example 1
Input
1 -> 2 -> 3 -> 4 -> 5 -> 6
Output
1

Explanation: The first half of the sequence is 1 -> 2 -> 3 and the second half is 4 -> 5 -> 6. Interleaving them results in 1 -> 4 -> 2 -> 5 -> 3 -> 6.

Example 2
Input
7 -> 8 -> 9
Output
7

Explanation: The first half of the sequence is 7 and the second half is 8 -> 9. Interleaving them results in 7 -> 9 -> 8.

Constraints

  • The sequence can contain any number of nodes.
  • The reversal should be done in-place, without using any additional data structures.
  • The time complexity should be O(n), where n is the number of nodes in the sequence.
  • The space complexity should be O(1), as only a constant amount of space can be used.
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