Show problems as
Classic problem #206EasyLinked ListRecursion

Reverse Linked List

Real-world version: Rewind the Telemetry Chain
Tracked in this browser only
Read Reverse Linked List on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstIn-Place Reversal Technique · Linked List, animated step by step

A probe stores its telemetry as a one-way chain of frames: every frame carries a signed reading and a link to the frame recorded after it, and the final frame links to nothing.

Because the harness passes plain JSON, the chain reaches you as the array frames, holding the readings in order from the front frame to the last one. An empty array means the chain has no frames at all. Your answer must use the same shape: the readings of the rebuilt chain, front frame first.

Turn the chain around so the frame that was last becomes the front and every link points at the frame that used to precede it. Solve it the way the chain itself demands: rebuild the frames, then walk them once and redirect one link at a time, carrying the frame behind you as you go. Beyond the frames themselves and the array you return, only a fixed number of frame references may be held.

Examples

Example 1

Input
frames = [4, -1, 9, 9, -12]
Output
[-12, 9, 9, -1, 4]

The frame holding -12 was last, so it becomes the front, and the readings come back in the opposite order. Repeated readings keep both copies.

Example 2

Input
frames = [30, -30]
Output
[-30, 30]

With two frames the single link between them flips, so the second frame leads.

Example 3

Input
frames = [7]
Output
[7]

A lone frame has no link to redirect, so the chain comes back unchanged. A chain of zero frames likewise answers with an empty chain.

Constraints

  • 0 <= frames.length <= 5000
  • -5000 <= frames[i] <= 5000

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.

Python
def rewind_telemetry_chain(frames: list[int]) -> list[int]:
Java
public int[] rewindTelemetryChain(int[] frames)