Solving tips
- First reduce k modulo n (k can be up to ~2e9); if k % n == 0 it's a no-op, return head unchanged.
- The cleanest trick: close the list into a ring by linking tail to head, then walk to the new tail and break it.
- The new tail sits at index n-k-1 (0-based), so from the head take exactly n-k-1 .next steps; be careful with this off-by-one.
- Handle empty/single-node lists up front; target O(n) time and O(1) space, and never return a still-cyclic list.
Problem
You are given the head of a singly linked list and an integer k. Rotate the list to the right by k places. Rotating right by one takes the last node and moves it to the front; doing this k times shifts every node k positions toward the tail, with nodes that fall off the end wrapping around to the front.
Return the head of the rotated list.
k can be much larger than the length of the list, so rotating by k is the same as rotating by k mod n, where n is the number of nodes.
Examples
Example 1
Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
Rotate right once β [5,1,2,3,4]; a second time β [4,5,1,2,3].
Example 2
Input: head = [0,1,2], k = 4
Output: [2,0,1]
n = 3, and 4 mod 3 = 1, so this is a single right rotation: the last node 2 moves to the front.
Example 3
Input: head = [], k = 5
Output: []
An empty list (or a single-node list) is unchanged by any rotation.
Constraints
- The number of nodes is in the range
[0, 500].
-100 <= Node.val <= 100
0 <= k <= 2 * 10^9 β so k may vastly exceed the list length; always reduce it modulo n.
Think about it first
Hint 1
Rotating right by k moves the last k nodes to the front. Which single node becomes the new head, and which node becomes the new tail?
Hint 2
First find the length n (one pass). Rotating by k is the same as rotating by k mod n. The new tail is the node at index n - k - 1 (0-based); the node right after it becomes the new head.
Hint 3
A clean trick: connect the tail to the head to form a ring, then walk forward to the new tail and break the ring there. The break point is n - (k mod n) steps from the original head.
TL;DR
Close the list into a ring, walk to the new tail, and cut β O(n) time, O(1) space.
Approach 1 β Brute force: rotate one step at a time
The naive idea mirrors the definition: to rotate right once, walk to the last node, detach it, and make it the new head. Repeat k times.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
from typing import Optional
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head or not head.next:
return head
for _ in range(k):
prev = None
curr = head
while curr.next:
prev = curr
curr = curr.next
# curr is the last node; move it to the front
prev.next = None
curr.next = head
head = curr
return head
Complexity: O(n Β· k) time, O(1) space. Each rotation walks the whole list, and k can be up to 2Β·10^9 β this times out immediately. The fix is to notice that rotating by k equals rotating by k mod n, and that a single rotation only relinks two pointers.
Approach 2 β Find the split point in two passes
The insight: rotating right by k (after reducing k mod n) means the last k nodes move to the front. Equivalently, the list is cut between index n - k - 1 and n - k (0-based): the node at n - k - 1 becomes the new tail, and the node at n - k becomes the new head. So: measure n, reduce k, walk to the new tail, and relink.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
from typing import Optional
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head or not head.next:
return head
# Pass 1: length and the old tail.
n = 1
old_tail = head
while old_tail.next:
old_tail = old_tail.next
n += 1
k %= n
if k == 0:
return head
# Pass 2: walk to the new tail at index n - k - 1.
new_tail = head
for _ in range(n - k - 1):
new_tail = new_tail.next
new_head = new_tail.next
new_tail.next = None
old_tail.next = head
return new_head
Walkthrough on Example 1 ([1,2,3,4,5], k = 2):
- Pass 1:
n = 5, old_tail = node 5.
k = 2 % 5 = 2, nonzero.
- New tail is at index
n - k - 1 = 2 β node 3. Walk two steps: 1 β 2 β 3.
new_head = new_tail.next = node 4. Cut: 3.next = None.
- Reconnect old tail:
5.next = head (1).
- Result:
4 β 5 β 1 β 2 β 3. β
Complexity: O(n) time (two passes), O(1) space.
Approach 3 β Close into a ring, then cut
The insight: if you first link the tail back to the head, the list becomes a circle where βrotateβ is just choosing where to break it. From the old tail, step forward n - (k mod n) nodes to reach the new tail, then sever. This avoids tracking two separate pointers for the cut.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
from typing import Optional
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head or not head.next:
return head
# Find length and close the ring.
n = 1
tail = head
while tail.next:
tail = tail.next
n += 1
tail.next = head # now it's a circle
# New tail sits steps_to_new_tail nodes from the head.
steps_to_new_tail = n - (k % n)
new_tail = head
for _ in range(steps_to_new_tail - 1):
new_tail = new_tail.next
new_head = new_tail.next
new_tail.next = None
return new_head
Walkthrough on Example 2 ([0,1,2], k = 4): n = 3; close ring 2 β 0. steps_to_new_tail = 3 - (4 % 3) = 3 - 1 = 2. Walk 2 - 1 = 1 step from head: new_tail = node 1. new_head = 1.next = 2; cut 1.next = None. Result 2 β 0 β 1. β
Complexity: O(n) time, O(1) space. When k % n == 0, steps_to_new_tail = n, so new_tail lands on the original tail and the list returns unchanged β the ring trick handles this without a special case.
Common pitfalls
- Not reducing
k: with k up to 2Β·10^9, forgetting k %= n either times out or overflows a step counter in stricter languages.
k % n == 0: the rotation is a no-op; return the original head. In the two-pass version you must guard this explicitly (otherwise you cut at the tail and drop the last nodeβs link).
- Empty or single-node list: return
head immediately; head.next is None and the length logic would need special casing otherwise.
- Off-by-one on the walk: the new tail is
n - k - 1 steps from the head (take n - k - 1 .next moves), not n - k. In the ring version, walk steps_to_new_tail - 1 times.
- Forgetting to break the ring: if you close the list into a circle you must sever
new_tail.next, or you return a cyclic list.
Pattern takeaway
For rotations and βmove the last k to the frontβ problems on linked lists, reduce k modulo the length first, then relink a constant number of pointers rather than moving nodes one at a time. Closing the list into a ring turns a rotation into a single choice of cut point β a reusable trick whenever a linear structure needs to wrap around.