Skip to content

visual walkthrough

Rotate List

MediumRing + CutReported at: MicrosoftBloomberg

Make a ring, then cut it at the right place.

Solve on LeetCode

The idea

Rotating right by k moves the last k nodes to the front. Rotating by the length n changes nothing, so only k mod n matters.

Join the tail to the head to make a ring, then cut it in the right place: the node n − k from the start becomes the new tail.

Complexity

approachtimespace
One step at a timeO(n × k)O(1)
Ring and cutO(n)O(1)

More walkthroughs