ExamAdda Logo

Rotate List

Easy

Given the head of a linked list, rotate the list to the right by k places.

A right rotation moves the last node to the beginning of the list.

If k is greater than the length of the list, rotate the list by k % n positions.

Example 1

Input

head = [1,2,3,4,5]
k = 2

Output

[4,5,1,2,3]

Explanation

The list is rotated right by 2 positions:

[1,2,3,4,5]    

     ↓

[4,5,1,2,3]

Example 2

Input

head = [0,1,2]
k = 4

Output

[2,0,1]

Explanation

The list has 3 nodes, so:

k % 3 = 4 % 3 = 1

 

Therefore, the list is rotated right by 1 position.

Constraints

0 <= number of nodes <= 500
-100 <= Node.val <= 100
0 <= k <= 2 * 10^9

Hints:

Hint 1

Find the length of the linked list first.

Hint 2

Since rotating by the list length produces the original list, reduce k using:

k = k % n

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 11, 2026

Last updated

Aug 12, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output