All problems
0185MediumLinked List

Flip a Span of the Gauge Chain

Tracked in this browser only
Write code

Trains the technique from

LeetCode 92Reverse Linked List II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def flip_chain_span(drifts: list[int], left: int, right: int) -> list[int]:
Java
public int[] flipChainSpan(int[] drifts, int left, int right)
September 7
Apply