Rotate List solution explained
To rotate a linked list right by k, count its length n, reduce k to k mod n, and cut the list after the first n - k nodes: that cut point becomes the new tail, the node after it becomes the new head, and the old tail is linked to the old head. This Rotate List solution takes O(n) time and O(1) extra space, and the modulo step is what keeps a huge k from costing anything extra.
By Evin JohnPublished 6 min read
The task, restated
You get the head of a singly linked list and a non-negative integer k. Shift every node k places to the right, wrapping around, so that the last node moves to the front k times. Return the new head. Nodes are reused; you only change next pointers.
| List | k | Result | Note |
|---|---|---|---|
| 10 → 20 → 30 → 40 → 50 → 60 | 2 | 50 → 60 → 10 → 20 → 30 → 40 | The last two nodes move to the front |
| 10 → 20 → 30 → 40 → 50 → 60 | 8 | 50 → 60 → 10 → 20 → 30 → 40 | 8 mod 6 = 2, same as above |
| 3 → 6 → 9 → 12 | 1 | 12 → 3 → 6 → 9 | One step right |
| 4 → 8 → 15 | 3 | 4 → 8 → 15 | k equals the length: no change |
| 7 | 100 | 7 | A single node never moves |
| empty | 5 | empty | Return null without counting |
Ask how large k can be. If it can reach into the billions, the interviewer is checking that you take the remainder before walking anything. Also confirm the direction: "right" means the tail comes to the front. A left rotation by k is a right rotation by n - (k mod n).
How interviewers use it
Rotate List is a pointer-bookkeeping question. There is no clever data structure; the test is whether you can find the right node, cut and re-join without losing part of the list or leaving a loop behind. It often appears as the first of two problems, so speed and tidiness matter as much as the idea.
- Rotate left instead. Convert to a right rotation with the formula above, or cut after k mod n nodes directly.
- Rotate an array in place. The three-reversal trick works there; on a list, re-linking is simpler.
- Do it in one pass. You cannot know k mod n without the length, so the honest answer is that a full count is needed whenever k may exceed n.
- Rotate a doubly linked list. The same cut, plus fixing two prev pointers.
Brute force: rotate one step at a time
A single right rotation finds the second-to-last node, detaches the last node and puts it in front. Repeating that k times works, but each step walks the whole list, so the cost is O(n·k), and a large k makes it hopeless. Reducing k mod n first caps it at O(n²). An array copy is another simple baseline: collect the nodes, rotate the array, re-link. That is O(n) time but O(n) extra memory.
def rotate_with_array(head, k):
nodes = []
while head:
nodes.append(head)
head = head.next
if not nodes:
return None
n = len(nodes)
k %= n
order = nodes[n - k:] + nodes[:n - k]
for a, b in zip(order, order[1:]):
a.next = b
order[-1].next = None
return order[0]O(1) space: count, then cut once
- Count and find the tailWalk to the last node, counting n. Return the head unchanged if the list is empty or has one node.
- Reduce kSet k to k mod n. If it is 0, the list is already in its rotated shape; return the head.
- Find the new tailStarting at the head, step n - k - 1 times. That node is the last of the part that stays at the back.
- Re-linkThe node after the new tail is the new head. Set the new tail's next to null and the old tail's next to the old head.
def rotate_right(head, k):
if head is None or head.next is None:
return head
n, tail = 1, head
while tail.next:
tail = tail.next
n += 1
k %= n
if k == 0:
return head
new_tail = head
for _ in range(n - k - 1):
new_tail = new_tail.next
new_head = new_tail.next
new_tail.next = None
tail.next = head
return new_headfunction rotateRight(head, k) {
if (!head || !head.next) return head;
let n = 1, tail = head;
while (tail.next) {
tail = tail.next;
n++;
}
k %= n;
if (k === 0) return head;
let newTail = head;
for (let i = 0; i < n - k - 1; i++) newTail = newTail.next;
const newHead = newTail.next;
newTail.next = null;
tail.next = head;
return newHead;
}A common variant joins the tail to the head first, making a ring, then walks n - k steps from the tail and breaks the ring there. It is the same work. The version above avoids the ring so there is never a moment when an early return could leave a cycle behind.
Time and space
| Approach | Time | Extra space |
|---|---|---|
| Rotate one step k times | O(n·k), or O(n²) after k mod n | O(1) |
| Copy nodes into an array | O(n) | O(n) |
| Count, then cut once | O(n): about 2n steps at worst | O(1) |
Edge cases
- Empty list: return null before computing k mod n, which would divide by zero.
- One node: any k leaves it alone.
- k = 0 or k a multiple of n: return the original head and change no pointers.
- k larger than n, including values far beyond any list size.
- k = n - 1: the new tail is the head itself, so the loop runs zero times.
Mistakes to avoid
- Taking k mod n before checking for an empty list.
- Walking n - k steps instead of n - k - 1, which lands on the new head rather than the new tail.
- Forgetting to null out the new tail's next, leaving a cycle that hangs any later traversal.
- Linking the old tail to the head when k mod n is 0, which also creates a cycle.
- Treating "rotate right" as moving the head to the back.
How to explain it out loud
Fictional example
- Candidate
- Rotating by the length changes nothing, so first I count. Six nodes, and 8 mod 6 is 2.
- Candidate
- So the last two nodes, 50 and 60, go to the front. The node before them, 40, becomes the tail. I reach it with 6 minus 2 minus 1, three steps from the head.
- Candidate
- Then 40's next becomes null, 60's next becomes 10, and I return 50. One counting pass and one partial pass, constant memory.
Related walkthroughs
Odd Even Linked List is another re-linking exercise where the danger is a dangling or circular next pointer. Restore IP Addresses shares the habit of ruling out impossible lengths before doing any work. For data structure topics beyond lists, see data structure interview questions.
Where Natively fits
In a practice session on Mac or Windows, Natively can take a screenshot of a list problem and lay out an approach with code in the overlay, a useful check after you have tried the count-and-cut steps yourself. If you transcribe a mock interview, the transcript shows whether you said why k mod n is safe. See the AI interview assistant page.
Questions people ask
How do you rotate a linked list to the right by k?
Count the nodes and find the tail, reduce k to k mod n, then walk n - k - 1 steps to the new tail. The next node is the new head; cut after the new tail and link the old tail to the old head.
Why take k mod n in Rotate List?
Rotating a list of n nodes by n puts every node back where it started, so only the remainder matters. It also keeps a huge k from costing extra time.
What is the time complexity of Rotate List?
O(n) time and O(1) extra space: one pass to count and find the tail, and at most one more partial pass to find the cut point.
What happens if k is a multiple of the list length?
The list is unchanged. Return the original head without touching any pointer, otherwise linking the tail to the head creates a cycle.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
