Reverse Linked List II
Real-world version: Flip a Span of the Gauge ChainThe statement here is our own: the same inputs, outputs and technique.
A pipeline is instrumented with gauges wired in a single direction: every gauge holds one signed drift reading and points at the gauge downstream of it, and the final gauge points at nothing.
The harness only carries plain JSON, so the chain reaches you as the array drifts, listing the readings from the first gauge through to the last. Your answer takes the same shape: the readings of the rewired chain, first gauge first.
Gauges are numbered from 1. Turn the run of gauges from position left through position right around, so the gauge that was last in that run now comes first and the gauge that was first in it now comes last. Everything outside the run keeps its place, and the run stays joined to whatever sat on either side of it.
Do this by rewiring, not by copying readings into a fresh chain: rebuild the gauges from the array, redirect the wires, and beyond those gauges and the array you hand back hold only a fixed number of gauge references. Walk the chain once.
Examples
Example 1
- Input
- drifts = [12, -4, 0, 7, 9, -1], left = 2, right = 5
- Output
- [12, 9, 7, 0, -4, -1]
Gauges at positions 2 through 5 carry -4, 0, 7 and 9; after rewiring that run reads 9, 7, 0, -4, while the gauges at positions 1 and 6 stay put.
Example 2
- Input
- drifts = [-8, 3], left = 1, right = 2
- Output
- [3, -8]
The run covers the whole chain, so the two gauges trade places.
Example 3
- Input
- drifts = [5, 5, -2, 5], left = 3, right = 3
- Output
- [5, 5, -2, 5]
A run of one gauge has nothing to turn around, so the chain is handed back unchanged.
Constraints
- The chain holds n gauges
- 1 <= n <= 500
- -500 <= drifts[i] <= 500
- 1 <= left <= right <= n
- Beyond the rebuilt gauges and the array you return, only a fixed number of gauge references may be held
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def flip_chain_span(drifts: list[int], left: int, right: int) -> list[int]:public int[] flipChainSpan(int[] drifts, int left, int right)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's