Browse curriculum

Rotate List

Rotate a linked list to the right by k places in one pass plus a walk: count the nodes, reduce k modulo n, close the list into a ring and cut it at the new tail.

Problem Understanding

Rotate List: given the head of a linked list, rotate it to the right by k places. Rotating right by one moves the last node to the front.

Examples: [1,2,3,4,5], k = 2 → [4,5,1,2,3]; [0,1,2], k = 4 → [2,0,1].

The Intuition: Only Two Links Change

After the rotation, the last k nodes come first, in the same order, followed by the first n - k nodes. So:

  1. Count the nodes, keeping the tail. Reduce k to k % n; if it is 0, nothing moves.
  2. Make a ring: the old tail now points to the old head.
  3. Find the new tail: it is the (n − k)-th node, n − k − 1 steps from head.
  4. Cut: the new head is the node after it, and its next becomes null.

Same idea as Rotate Array, but on a list only links change. Finding a node a fixed distance from the end also shows up in Remove Nth Node From End of List.

Interactive Walkthrough

ROTATE RIGHT BY k

head

0

#0

→

1

#1

→

2

#2

→ NULL

n

:

k

4

walk n−k−1

:

 

Rotate right by k = 4: the last k nodes move to the front

Next

tail starts at head #0 (0), n = 1

head

k

4

Legend & Complexity

tail while counting, newTail while finding the cut

The new head, right after the cut

The rotated list

↺ back to #0: the tail linked to the head, a ring

Time

O(n)

Space

O(1)

Watch the row end change to ↺ back to #0 when the ring closes, and the row start at the new head after the cut. Try Big k to see the modulo at work.

Dry Run Table

Input: [1,2,3,4,5], k = 2.

Step What happens
Count tail = 5, n = 5
Modulo k = 2 % 5 = 2
Ring 5 → 1
Walk n − k − 1 = 2 steps: newTail = 3
Cut newHead = 4; 3 → null

Answer: [4,5,1,2,3].

The Solution Template

Count, modulo, ring, walk, cut.

Rotate List Code
1var rotateRight = function(head, k) {
2 if (!head || !head.next || k === 0) return head;
3 let tail = head, n = 1;
4 while (tail.next) { tail = tail.next; n++; }
5 k %= n;
6 if (k === 0) return head;
7 tail.next = head;
8 let newTail = head;
9 for (let i = 0; i < n - k - 1; i++) newTail = newTail.next;
10 const newHead = newTail.next;
11 newTail.next = null;
12 return newHead;
13};

Edge Cases & Common Mistakes

  • k ≥ n: always reduce with k % n first.
  • k % n = 0: return head before making the ring, or remember to break it.
  • Empty list or one node: return head; there is nothing to rotate (and n would be 0 in the modulo).
  • Off by one: the new tail is n − k − 1 steps from head, not n − k.
  • Rotating left instead: right by k is left by n − k.

Summary

Count n and keep the tail, reduce k to k % n, link the tail to the head, walk n − k − 1 steps to the new tail, and cut after it. O(n) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Count

    Walk to the tail, counting n. Set k = k % n and return head if it is 0.

  2. Make a ring

    Link the tail's next to the head.

  3. Cut

    Walk n - k - 1 nodes from the head to the new tail; its next is the new head; set its next to null.

Frequently asked questions

Why take k modulo n?

Rotating a list of n nodes by n puts every node back where it started, so only k % n matters. This also handles k far larger than the list (LeetCode allows k up to 2 × 10⁹) without walking it billions of times.


Why does the new tail sit n - k - 1 nodes from the head?

Rotating right by k moves the last k nodes to the front, so the first n - k nodes stay in order and end the new list. The last of them, the new tail, is at position n - k - 1 counting from 0, which is n - k - 1 steps from the head. The node after it becomes the new head.


Why link the tail to the head before cutting?

It joins the old end to the old start in one step. After that the list is a ring, and a single cut after the new tail gives the rotated list. Without the ring you would cut the list and then relink the old tail to the old head separately, which is the same work in a different order.


How is this different from Rotate Array?

Arrays rotate by moving values, usually with three reversals. A linked list rotates by changing two links: the old tail's next becomes the old head, and the new tail's next becomes null. No values move.

New to the technique behind this problem? Watch it run step by step:

Singly Linked List Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description