Rotate a Linked List to the Right

You are given the head of a singly linked list (as an array of node values) and a non-negative integer k. Rotate the list to the right by k places and return…

You are given the head of a singly linked list (as an array of node values) and a non-negative integer `k`. Rotate the list to the **right** by `k` places and return the new head. Rotating to the right by one place means the last node moves to the front: for `[1, 2, 3, 4, 5]` a rotation of 1 gives `[5, 1, 2, 3, 4]`. A rotation of `k` moves the last `k` nodes to the front in order. Because `k` may exceed the list length `n`, the **effective rotation** is `k % n`. If `k % n = 0` the list is returned unchanged. **Indexing convention:** this platform uses 0-based indexing throughout. The new tail after rotation is at position `n − k − 1` from the original head.

- `1 <= nodes.length <= 500` - `-100 <= nodes[i] <= 100` - `0 <= k <= 2 × 10^9` - Rotate RIGHT by k (the last k nodes move to the front). - Effective rotation is k % n; if k % n = 0 the list is unchanged.

Example 1: input head = [1,2,3,4,5], k = 2; output [4, 5, 1, 2, 3]. n = 5, k % 5 = 2. The last 2 nodes (4, 5) move to the front. New tail is at index 5 − 2 − 1 = 2 (value 3); new head is node 3 (value 4).

Example 2: input head = [1,2,3,4,5], k = 7; output [4, 5, 1, 2, 3]. n = 5, 7 % 5 = 2. Same effective rotation as k = 2.

Example 3: input head = [1,2,3,4,5], k = 0; output [1, 2, 3, 4, 5]. k = 0 means no rotation — the list is returned unchanged.

Category: Linked List. Topic: linked-list. Difficulty: Medium. Patterns: linked-list-traversal.