Given a list of integers and a positive integer n, return a new list with the nth element from the end removed. For example, with n equal to 1, remove the last element. With n equal to 2, remove the second-to-last element. If n is null, less than or equal to zero, or greater than the list size, return the list unchanged. Return null for a null list.
Examples:
Input: nums = new List<Integer>{1, 2, 3, 4, 5}, n = 2
Output: [1, 2, 3, 5]
Explanation: The second element from the end is 4, so it is removed
Input: nums = new List<Integer>{1, 2, 3, 4, 5}, n = 1
Output: [1, 2, 3, 4]
Explanation: The last element (5) is removed
Input: nums = new List<Integer>{1, 2, 3, 4, 5}, n = 5
Output: [2, 3, 4, 5]
Explanation: The fifth element from the end is the first element (1)
Input: nums = new List<Integer>{7}, n = 1
Output: []
Explanation: Single element list becomes empty after removing the last element
Input: nums = null, n = 1
Output: null
Explanation: Null list 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.