Search for a command to run...
You are given a singly linked list of integers.
Divide the list into contiguous segments such that:
Each segment contains only even values OR
Each segment contains only odd values
For each segment:
If the length is even → Reverse the segment
If the length is odd → Rotate the segment to the right by one position
Maintain the relative order of segments
Perform all operations in-place
Return the head of the modified linked list
Head of a singly linked list
Nodes represented as: value → value → value → ...
Modified linked list after applying transformations
1≤N≤1000001 \le N \le 1000001≤N≤100000
1≤Node value≤100001 \le \text{Node value} \le 100001≤Node value≤10000
Input:
1 → 3 → 5 → 2 → 4 → 6Output:
5 → 1 → 3 → 6 → 2 → 4Explanation:
Segment 1: [1,3,5] (odd values, length = 3 → rotate right)
Segment 2: [2,4,6] (even values, length = 3 → rotate right)
Example 1
10, 20, 30, 4040, 30, 20, 10Example 2
2, 4, 6, 8, 1010, 2, 4, 6, 8Example 3
2, 4, 1, 34, 2, 3, 110, 20, 30, 40
40, 30, 20, 10