Show problems as
Classic problem #61MediumLinked ListTwo Pointers

Rotate List

Real-world version: Rundown Cue Shift
Tracked in this browser only
Read Rotate 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 radio show keeps its rundown as a one-way chain of cue cards. Each card carries a signed timing offset in seconds and a link to the card that comes after it, and the final card links to nothing.

Because the harness passes plain JSON, the chain reaches you as the array cues, listing the offsets from the leading card through to the final one. An empty array means there is no rundown at all. Your answer must come back in the same shape: the offsets of the rearranged chain, leading card first.

The producer wants the rundown wound forward by shift places. One place forward means the final card becomes the leader and everything else slides back one spot; shift places forward means the last shift cards move ahead of the rest as a block, with their own order among themselves untouched. The value of shift can run far past the number of cards, in which case the winding simply comes around the chain again as many times as it needs to. A rundown of no cards, or of one card, comes back exactly as it arrived.

Do the work the chain calls for: hold the rundown as linked cards and move cards by changing which card each one links to. Do not build the answer by sorting the offsets or by pasting slices of the array together.

Examples

Example 1

Input
cues = [6, -2, 9, 4, -7], shift = 2
Output
[4, -7, 6, -2, 9]

Winding forward twice moves the final two cards, holding 4 and -7 in that order, ahead of the other three.

Example 2

Input
cues = [3, 8, -5], shift = 7
Output
[-5, 3, 8]

Seven places around a chain of three cards is two full laps plus one more place, so only the final card ends up in front.

Example 3

Input
cues = [], shift = 4
Output
[]

There are no cards to wind, so the rundown stays empty however large the shift is.

Constraints

  • 0 <= cues.length <= 500
  • -100 <= cues[i] <= 100
  • 0 <= shift <= 2 * 10^9

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 rundown_cue_shift(cues: list[int], shift: int) -> list[int]:
Java
public int[] rundownCueShift(int[] cues, int shift)