Reorder List
Real-world version: Fold the Signal Relay ChainThe statement here is our own: the same inputs, outputs and technique.
A radio link runs through repeater stations coupled in one direction: each station carries an integer gain and points at the station downstream of it, and the last station points at nothing.
Because the harness passes plain JSON, the link arrives as the array stations, holding the gains from the head station through to the tail station. Your answer takes the same shape: the gains of the recoupled link, head station first.
Re-cable the link so it alternates between the two ends. The head station leads, the tail station follows it, then the second station, then the second-from-last, and so on until every station has been placed exactly once. Only the couplings change: no station is duplicated, dropped, or has its gain rewritten.
Do the work on the couplings themselves rather than by copying gains into a fresh link. Rebuild the stations from the array, then relink them, and beyond those stations and the array you hand back keep only a fixed number of station references.
Examples
Example 1
- Input
- stations = [2, 6, 1, 9]
- Output
- [2, 9, 6, 1]
The head station keeps the lead, the tail station couples behind it, then the second station, and the second-from-last station closes the link.
Example 2
- Input
- stations = [10, 20, 30, 40, 50, 60]
- Output
- [10, 60, 20, 50, 30, 40]
Three stations from the front interleave with three from the back, so the two halves zip together.
Example 3
- Input
- stations = [5, 1, 4, 1, 5]
- Output
- [5, 5, 1, 1, 4]
An odd count leaves one station unpaired, so the middle station with gain 4 ends up at the tail.
Constraints
- 1 <= stations.length <= 5 * 10^4
- 1 <= stations[i] <= 1000
- Beyond the rebuilt stations and the returned array, only a fixed number of station 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 fold_relay_chain(stations: list[int]) -> list[int]:public int[] foldRelayChain(int[] stations)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 9 test cases: 9 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