Rotate List Visualization & Animation

Rotates a linked list right by k places by making it circular and cutting at the new tail; O(n).

## What is it? Rotate a singly linked list to the right by `k` places. Each rotation moves the last element to the front. ## How it works - Compute the length `n` of the list; make it circular (connect tail to head) - Effective rotation: `k = k % n` (rotations of exactly `n` do nothing) - The new tail is at position `n - k - 1` (0-indexed from head); the new head is its next - Break the circle at the new tail: `new_tail.next = null` ## When to use - Array/list rotation problems - Cyclic shift operations on sequences ## Key Points - O(n) time, O(1) space - Always reduce `k` modulo `n` to avoid unnecessary full rotations - Making the list circular and then cutting at the right point is cleaner than repeatedly moving the tail

Category: algorithms

Difficulty: intermediate

Time Complexity: O(n)

Space Complexity: O(1)

Rotate List

intermediate

Rotates a linked list right by k places by making it circular and cutting at the new tail; O(n).

Start
null1384712042215638473492121564156749215NULL1567tailnewTailcurrhead
Rotate [1, 2, 3, 4, 5] right by 2 steps. Step 1: walk the list to find its length and tail node.