Rotate List
Rotate List is LeetCode 61 (Medium). Given the head of a linked list and a number k, rotate the list to the right by k places: each rotation moves the last node to the front. Return the new head.
- The list can be empty and holds at most 500 nodes.
kcan be as large as 2 × 10⁹, far more than the number of nodes, so doing rotations one at a time is out.
- The number of nodes in the list is in the range [0, 500].
- -100 <= Node.val <= 100
- 0 <= k <= 2 * 10^9
Intuition
Rotating a list of n nodes n times puts it back where it started, so only k mod n rotations matter. Rotating right by r simply moves the last r nodes, as one piece, to the front.
Join the tail to the head to make a ring, then cut the ring in a new place. In a ring every node has a successor, so the order never changes; only the cut decides where reading starts. The new head is the node r places from the end, which is n − r nodes from the old head, and the node just before it becomes the new tail.
"Shift a list by k" with k allowed to exceed the length: reduce k modulo the length, then think of the list as a circle that is cut once.
Approach
Before reading on, work out which node becomes the new tail when a 5-node list is rotated by 2, counted as steps from the head. Then check your formula with k = 7 and with k = 5.
Count the nodes and find the tail
Return the head if the list is empty. Otherwise walk to the last node, counting as you go. You need the tail anyway to close the ring, so the count costs no extra pass.
Reduce k
Set k = k % n. If that is 0, the rotation lands where it started, so return the head unchanged. This also covers a one-node list, where every k is a multiple of 1.
Close the ring
Point the tail at the head. Now the nodes form a circle in the original order, and any node can be chosen as the start by breaking the link in front of it.
Walk to the new tail and cut
From the head, take n − k − 1 steps to reach the new tail. Its next node is the new head. Set the new tail's next to null to break the ring, and return the new head.
Code in Python, C++ and Java
Common pitfalls
Rotating one node at a time
for _ in range(k):
move the last node to the frontk %= n # then one cut
Each single rotation walks the whole list to find the second-to-last node. With k = 2 × 10⁹ that is billions of walks, even on a 3-node list where k % 3 would be 2.
Taking n − k steps instead of n − k − 1
for _ in range(n - k):
for _ in range(n - k - 1):
Starting on the head already counts as standing on node 1. On [1, 2, 3, 4, 5] with k = 2, three steps stop on 4 instead of 3, so the list is cut one place too late: [5, 1, 2, 3, 4] instead of [4, 5, 1, 2, 3].
Forgetting to break the ring
new_head = new_tail.next return new_head
new_head = new_tail.next new_tail.next = None return new_head
The tail was joined to the head, so without the cut the returned list has no end. Printing [1, 2, 3] rotated by 1 would loop 3, 1, 2, 3, 1, 2 … forever.
Edge cases
k % n is 0, so the head is returned before the ring is closed: [1, 2] with k = 2 comes back as [1, 2].
Only the remainder counts. [0, 1, 2] with k = 4 is the same as k = 1: [2, 0, 1].
An empty list returns at once. One node gives k % 1 = 0, so it is returned unchanged.
Complexity
n steps to the new tail. The size of k does not matter after the modulo. Only a few pointers are stored.Rotate List FAQ
How do you rotate the list to the left instead?
Rotating left by k is rotating right by n − k. Reduce k modulo n, then take k − 1 steps from the head to find the new tail and cut there.
Is there a version without closing the ring?
Yes. After reducing k, start a second pointer k nodes ahead of the first and move both until the front one is on the tail. The back one is the new tail. It is the same amount of work; the ring version just needs fewer special cases.
How is this different from rotating an array?
An array has to move every element, usually with three reversals. A list moves nothing: one link is added from tail to head and one is removed at the cut.