#61 Rotate List
the problem
In plain words
Rotate a linked list to the right by k places: one rotation moves the last node to the front. k can be far larger than the length of the list.
in head = [1,2,3,4,5], k = 2
out [4,5,1,2,3]
in head = [0,1,2], k = 4
out [2,0,1]
Rotating 3 times gives back [0,1,2]; one more gives [2,0,1].
- 0 ≤ number of nodes ≤ 500
- -100 ≤ Node.val ≤ 100
- 0 ≤ k ≤ 2·10⁹
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternIn-place linked-list rewiring
Rotating n times changes nothing, so only k % n matters. Join the tail to the head to make a ring, then cut the ring open in the right place: the new tail is n − k − 1 hops from the old head.
solutions
From brute force to optimal
Do k % n single rotations; each walks to the second-to-last node and moves the last node to the front.
- Count n; reduce k to k % n.
- Repeat k times: walk to the second-to-last node, cut the last node off, put it in front.
Each single rotation walks the whole list; there are up to n − 1 of them.
A few references.
class Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null || head.next == null) return head; int n = 0; for (ListNode p = head; p != null; p = p.next) n++; for (int r = 0; r < k % n; r++) { // without % n, k = 2·10⁹ would never finish ListNode beforeLast = head; while (beforeLast.next.next != null) beforeLast = beforeLast.next; // walk to the 2nd-last node ListNode last = beforeLast.next; beforeLast.next = null; // cut the last node off... last.next = head; // ...and put it in front head = last; } return head; }}Count n and find the tail in one pass; k %= n; tail.next = head; walk n − k − 1 steps to the new tail; cut after it.
- Empty or one node → return head.
- Walk to the tail counting n; k %= n; if k == 0 return head.
- tail.next = head (a ring).
- newTail = n − k − 1 hops from head; newHead = newTail.next; newTail.next = null.
One pass to count, at most one more to reach the new tail.
A few references; nodes are re-linked, not copied.
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) { // count the nodes and find the tail tail = tail.next; n++; } k %= n; // rotating by n changes nothing if (k == 0) return head; tail.next = head; // close the list into a ring ListNode newTail = head; for (int i = 0; i < n - k - 1; i++) newTail = newTail.next; // n − k − 1 hops from head ListNode newHead = newTail.next; newTail.next = null; // cut the ring open at the new tail return newHead; }}in the room
How to say it in the interview
talk track
- First say: "k can be 2·10⁹, so I'll reduce it with k % n — and that needs the length."
- Counting the length also finds the tail — exactly what the ring needs.
- Handle the empty list, one node, and k % n == 0 up front.
traps
- Skipping
k % n→ up to 2·10⁹ rotations. - Computing
k % nbefore checking for an empty list →ArithmeticException(division by zero). - Forgetting to cut the ring (
newTail.next = null) → a list that never ends.
what they ask next
Follow-ups
Rotate left by k?
Rotating left by k is rotating right by (n − k % n) % n — reuse the same code.
Rotate an array instead (LC 189)?
Arrays can't be re-linked, so use three reversals: reverse all, then the first k, then the rest — O(n) time, O(1) space.
before you submit
Edge cases to test
- Empty list → null (and avoid k % 0).
- k = 0 or k a multiple of n → unchanged.
- One node → unchanged for any k.
- k = 2·10⁹ → only k % n matters.
practise next
Similar problems
- #189Rotate ArrayMediumin Arrays & Strings
- #19Remove Nth Node From End of ListMediumin Linked Lists