Rotate List
Shifting right by s (where s = k % n) is the same as cutting the list at one place. Count the nodes and find the tail. Join the tail to the head to make a circle. A circle has no start, so cut it once, right after the node that will end the new list. Shifting the circle changes nothing except where you cut it.
The answer is the circle read from the node n − (k % n) steps from the old head, with the link just before that node cut.
Problem
You are given the head of a singly linked list and a number k. Move the list k places to the right. Each shift takes the last node and puts it at the front. Return the new head.
The list is changed by relinking nodes. Do not copy values into new nodes.
head, the first node of a singly linked list (it can be empty), and k, a number that is zero or more.
The head of the rotated list.
Two shifts: first 5 moves to the front, then 4 does. The last two nodes end up at the front.
There are only 3 nodes. Three shifts give the same list back, so 4 shifts is the same as 1 shift: 2 moves to the front.
- • The list has between 0 and 500 nodes.
- • -100 ≤ Node.val ≤ 100
- • 0 ≤ k ≤ 2 × 10^9
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If k = 1, which node becomes the new head? What about k = 2?
- 2.
What happens when k is bigger than the number of nodes? Do I really need to shift that many times?
- 3.
Where does the new head sit, counting from the old head? Can I find that spot with one walk?
- 4.
If I join the tail back to the head, what shape does the list have? Where would I cut it?
- 5.
When k is an exact multiple of the length, what should the answer look like?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Repeat k % n times: walk to the last node and move it to the front | O(n · (k % n)) | O(1) |
| Optimal | Count n, join the tail to the head, walk n − k%n − 1 steps and cut | O(n) | O(1) |
Why does this belong to this pattern?
- “rotate the list to the right by k”The tail wraps around to the front → join the tail to the head to make a circle, then cut it.
- “k can be bigger than the length”A full turn changes nothing → use k % n, never k itself.
- “singly linked list, return the new head”Only one link changes → relink nodes, no copying of values.
Reordering here means splitting one list at one point and putting the two parts in swapped order. Rotate List is the simplest reorder: one cut is enough. The join is a helper: it lets the cut land anywhere on the circle. Odd Even Linked List and Reorder List later need several relinks in turn, not just one join and one cut.
Don't make this mistake
k can be far bigger than n, up to 2 × 10^9. A full turn changes nothing, so only k % n matters. Without it, the cut position is negative or past the end, and the answer is wrong or the program crashes.
When k % n is 0, the loop walks one node too far. newTail lands on the old head, so the answer comes out shifted by one (2 3 4 5 1 for k = 5). Check for the zero case before cutting.
Pattern confusion: an array can rotate by moving values in place, but a linked list rotates by moving one link. Repeating the array idea walks the whole list on every shift. On the dry run (1 → 2 → 3 → 4 → 5, k = 7), this loop does 7 shifts, and each one walks all 5 nodes: 35 steps. Only 7 % 5 = 2 shifts change the answer, and the one-pass plan takes 7 node visits (5 to count, 2 to reach the cut). With k near 2 × 10^9 the loop is far too slow.
Joining the tail to the head makes a circle. If you return without cutting the link after newTail, the list never ends, and any traversal loops forever. The cut is a required link change, not an optional step.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.