Given an ArrayList of integers nums, swap its first and last numbers and return it.
Change nums itself, then return it. The numbers in between stay where they are. When nums has only one number, that number is both the first and the last, so it stays as it is.
Example 1:
Input: nums = [1, 2, 3, 4, 5] Output: [5, 2, 3, 4, 1] Explanation: 1 and 5 trade places. 2, 3 and 4 don't move.
Example 2:
Input: nums = [4, 9] Output: [9, 4] Explanation: With two numbers, the first and the last are next to each other, and they trade places.
Example 3:
Input: nums = [7] Output: [7] Explanation: One number is both the first and the last, so nothing changes.
Constraints:
numshas at least one number.- Every number is an integer between -1000 and 1000.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
