Given a list of integers and a non-negative number k, rotate the list to the right by k positions and return a new list. The original list must not be modified. When k is larger than the size of the list, the effective rotation is k modulo the list size. Return null when the input list is null or k is negative. Return an empty list when the input list is empty.
Examples:
Input: nums = [1, 2, 3, 4, 5], k = 2
Output: [4, 5, 1, 2, 3]
Explanation: The last 2 elements move to the front.
Input: nums = [1, 2, 3], k = 0
Output: [1, 2, 3]
Explanation: A rotation of 0 returns the original order.
Input: nums = [1, 2, 3], k = 4
Output: [3, 1, 2]
Explanation: 4 modulo 3 equals 1, so rotate right by 1 position.
Input: nums = []
Output: []
Explanation: An empty list rotates to an empty list.
Input: nums = null
Output: null
Explanation: A null input returns null.
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.