BackhardLinked List

Invert Interspersed Nodes Solution

Problem Statement

Given a singly linked list, reverse every alternate set of 3 nodes. For example, if the input is 1 -> 2 -> 3 -> 4 -> 5 -> 6, the output should be 3 -> 2 -> 1 -> 6 -> 5 -> 4.

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

Explanation: The list is split into two sets of 3 nodes each. The first set (1 -> 2 -> 3) is reversed to 3 -> 2 -> 1, and the second set (4 -> 5 -> 6) is reversed to 6 -> 5 -> 4.

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

Explanation: The list is split into two sets. The first set (7 -> 8 -> 9) is reversed to 9 -> 8 -> 7, and the second set (10 -> 11) is left as is because it has only 2 nodes.

Constraints

  • 1 <= number of nodes <= 1000
  • 1 <= node value <= 10000
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