LeetCode #61 Medium

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.
  • k can be as large as 2 × 10⁹, far more than the number of nodes, so doing rotations one at a time is out.
Constraints
  • The number of nodes in the list is in the range [0, 500].
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 10^9
linked-listtwo-pointers
Open on LeetCode ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
▶3 if not head:
▶4 return head
▶5 n, tail = 1, head
▶6 while tail.next:
▶7 tail = tail.next
▶8 n += 1
▶9 k %= n
▶10 if k == 0:
▶11 return head
▶12 tail.next = head
▶13 new_tail = head
▶14 for _ in range(n - k - 1):
▶15 new_tail = new_tail.next
▶16 new_head = new_tail.next
▶17 new_tail.next = None
▶18 return new_head
1head, tail2345length1turnstail starts on the head; length 1 so far
length1
k2
The last node points to nothing, so the circle has a gap between 5 and 1. Walking to the tail counts the nodes on the way.
1head2tail345length2turnstail moves to 2; length 2
tail2
length2
Not the end yet: 2 still has a next node.
1head23tail45length3turnstail moves to 3; length 3
tail3
length3
Not the end yet: 3 still has a next node.
1head234tail5length4turnstail moves to 4; length 4
tail4
length4
Not the end yet: 4 still has a next node.
1head2345taillength5turnstail moves to 5; length 5
tail5
length5
5 has no next node, so it is the tail and the list has 5 nodes.
1head2345taillength5turns2the last 2 move to the front
k2
turns left2
Every 5 rotations bring the list back to the start, so only the remainder, 2, matters.
1head2345taillength5turns2join 5 back to 1: the list is a ring
5 points to1
Now every node has a next node and the order is still the original one. Removing any single link turns it back into a list starting after that link.
1new tail2345length5turns2new tail starts on 1; 2 steps to go
steps2
The new tail is the node just before the last 2, which is node 3 from the head. Standing on the head already counts as node 1, so 5 − 2 − 1 = 2 steps remain.
12new tail345length5turns2new tail moves to 2
new tail2
steps left1
One step done, 1 to go.
123new tail45length5turns2new tail moves to 3: stop here
new tail3
steps left0
The node after 3 is 4, the first of the nodes moving to the front.
123new tail4new head5length5turns2cut after 3: 4 is the new head
new head4
3 points tonothing
Breaking the link from 3 gives the ring an end again. Reading starts at 4 and runs all the way around to 3.
1234new head5length5turns2return 4 → 5 → 1 → 2 → 3
return4
Only two links changed in total: the closing one from 5 to 1 and the cut after 3.
05

Common pitfalls

Rotating one node at a time

✗ Wrong
for _ in range(k):
    move the last node to the front
✓ Right
k %= 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

✗ Wrong
for _ in range(n - k):
✓ Right
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

✗ Wrong
new_head = new_tail.next
return new_head
✓ Right
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.

06

Edge cases

k is a multiple of the length

k % n is 0, so the head is returned before the ring is closed: [1, 2] with k = 2 comes back as [1, 2].

k larger than the length

Only the remainder counts. [0, 1, 2] with k = 4 is the same as k = 1: [2, 0, 1].

Empty list or one node

An empty list returns at once. One node gives k % 1 = 0, so it is returned unchanged.

07

Complexity

Time
O(n)
Space
O(1)
One walk to count and find the tail, then fewer than n steps to the new tail. The size of k does not matter after the modulo. Only a few pointers are stored.
08

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.