Rotate List
Pattern: Traversal and dummy nodes
Rotating right by k moves the last k mod n nodes to the front. Find the length and the tail, cut after node n - k, and join the old tail to the old head.
The problem
Given the head of a linked list and a whole number k ≥ 0, rotate the list to the right by k places: each rotation
moves the last node to the front. Return the new head. For example, 1 → 2 → 3 → 4 → 5 with k = 2 becomes
4 → 5 → 1 → 2 → 3, and 0 → 1 → 2 with k = 4 becomes 2 → 0 → 1.
The function name rotateRight matches LeetCode. Use the link above for the exact statement and limits. k can be up to
2 × 10⁹, far more than the length.
Think first
- After
nrotations, what does the list look like? - After
krotations, which node is the new head? - How many pointers change?
Hints
Brute force, and why it fails
Move the last node to the front k times: O(n × k), hopeless for huge k. Using k mod n and one cut is O(n).
The key insight
A rotation is one cut and one join on a ring. Link the list into a ring in your head, and only the place of the cut
depends on k.
Dry run
| n | 5 |
|---|---|
| k | 2 |
The list is 1 → 2 → 3 → 4 → 5 and k = 2. Walk to the tail and count n = 5.
The plan
if head is null or head.next is null: return head
n = length, tail = last node
k = k mod n; if k == 0: return head
newTail = node number n - k
newHead = newTail.next
newTail.next = null
tail.next = head
return newHead
Complexity
- Time
O(n)- Space
O(1)
At most two walks.
Edge cases
ka multiple ofn: unchanged.- Empty or one node: unchanged.
- Huge
k: reduce it first.
I've built my logic, show the code
Java
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) return head;
int n = 1;
ListNode tail = head;
while (tail.next != null) { tail = tail.next; n++; } // length and last node
k %= n;
if (k == 0) return head;
ListNode newTail = head;
for (int i = 1; i < n - k; i++) newTail = newTail.next; // node n - k becomes the new tail
ListNode newHead = newTail.next;
newTail.next = null; // cut there
tail.next = head; // and join the old end to the old start
return newHead;
}
}C++
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if (head == NULL || head->next == NULL) return head;
int n = 1;
ListNode* tail = head;
while (tail->next != NULL) { tail = tail->next; n++; } // length and last node
k %= n;
if (k == 0) return head;
ListNode* newTail = head;
for (int i = 1; i < n - k; i++) newTail = newTail->next; // node n - k becomes the new tail
ListNode* newHead = newTail->next;
newTail->next = NULL; // cut there
tail->next = head; // and join the old end to the old start
return newHead;
}
};Python
from lib import ListNode
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if head is None or head.next is None:
return head
n, tail = 1, head
while tail.next is not None: # length and last node
tail = tail.next
n += 1
k %= n
if k == 0:
return head
new_tail = head
for _ in range(n - k - 1): # node n - k becomes the new tail
new_tail = new_tail.next
new_head = new_tail.next
new_tail.next = None # cut there
tail.next = head # and join the old end to the old start
return new_headCommon mistakes
- Not reducing
k, which loops for billions of steps. - Cutting at node
kinstead of noden - k. - Forgetting to join the old tail to the old head.