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
2, 4, 1, 34, 2, 3, 1Example 2
2, 4, 6, 8, 1010, 2, 4, 6, 8Example 3
10, 20, 30, 4040, 30, 20, 102, 4, 1, 3
4, 2, 3, 1