Search for a command to run...
You are given a singly linked list of integers.
Identify continuous segments of nodes such that:
The sum of values in that segment is even
For each such segment:
Reverse the nodes within that segment
Keep the rest of the list unchanged
Each segment must be as large as possible while keeping the sum even
All reversals must be done in-place
Return the head of the modified linked list
Head of a singly linked list
Nodes are represented as: value → value → value → ...
Modified linked list after performing required reversals
1≤N≤1000001 \le N \le 1000001≤N≤100000
1≤Node value≤100001 \le \text{Node value} \le 100001≤Node value≤10000
Input:
1 → 2 → 3 → 4 → 5Output:
1 → 2 → 3 → 4 → 5Explanation: No large segment with even sum that benefits reversal.
Example 1
2, 3, 5, 7, 8, 102, 3, 5, 7, 10, 8Example 2
1, 2, 3, 4, 6, 11, 2, 3, 6, 4, 1Example 3
1, 3, 5, 73, 1, 7, 52, 3, 5, 7, 8, 10
2, 3, 5, 7, 10, 8