Problem overview
The function swapFirstAndLast receives a list of integers, nums, swaps its first and last numbers and returns the list. It changes nums itself, and the numbers in between stay where they are. When the list has one number, that number is both first and last, so nothing changes.
For [1, 2, 3, 4, 5] the answer is [5, 2, 3, 4, 1]. For [4, 9] it is [9, 4]. For [7] it is [7].
Approach
Two values can't trade places in a single move. If you write the last number into the first place right away, the old first number is gone. The standard answer is a temporary variable: save one value, overwrite its place, then put the saved value in the other place.
The parameter type matters here. MutableList<Int> is a list that can be changed in place. A plain List<Int> is read-only: assigning to nums[0] on it is a compile error, because there is no set operator for it.
Step by step
- Save the first number in a
valnamedtemp:nums[0]. The next step overwrites that place, so its old value would be lost otherwise. - Copy the last number into the first place:
nums[0] = nums[nums.size - 1]. The last index is the size minus 1. - Put the saved number in the last place:
nums[nums.size - 1] = temp. - Return
nums. The same list comes back, now swapped.
The solution in Kotlin
class Solution {
fun swapFirstAndLast(nums: MutableList<Int>): MutableList<Int> {
val temp = nums[0]
nums[0] = nums[nums.size - 1]
nums[nums.size - 1] = temp
return nums
}
}
Details worth knowing:
- The list is changed in place. The function doesn't build a new list. The returned list is the same object that was passed in, so a caller that keeps its own reference sees the swap too. After calling the function on a list
aholding[1, 2, 3, 4, 5], both the result andaprint as[5, 2, 3, 4, 1], andresult === aistrue. - Indexing assignment.
nums[0] = valueis shorthand for callingset(0, value)on the list. valfor the temporary. The saved number is never reassigned, sovalfits.- A built-in alternative. Kotlin on the JVM can call Java's
java.util.Collections.swap(nums, 0, nums.size - 1), which does the same job. The lesson writes the swap by hand to show the temporary variable.
Tracing the examples
For [1, 2, 3, 4, 5]: temp becomes 1. Then nums[0] becomes 5, and the list is [5, 2, 3, 4, 5]. Finally nums[4] becomes 1, and the list is [5, 2, 3, 4, 1].
For [4, 9]: the size is 2, so the last index is 1. The two numbers trade places and the list becomes [9, 4].
For [7]: the first and last index are both 0. temp is 7, the list gets 7 at index 0, then temp (7) at index 0 again. The list is still [7], so one-number lists need no special case.
Edge cases and common mistakes
- Skipping the temporary. Writing
nums[0] = nums[nums.size - 1]and thennums[nums.size - 1] = nums[0]copies the last number into both places, becausenums[0]was already overwritten. - Using
nums.sizeas the last index. It is one past the end and throws anIndexOutOfBoundsException. - An empty list. The lesson promises at least one number. On an empty list,
nums[0]throws anIndexOutOfBoundsException. - Read-only lists. The signature uses
MutableListbecause the function writes to the list. Declaring the parameter asList<Int>would stop the code from compiling.
Testing
[1, 2, 3, 4, 5]gives[5, 2, 3, 4, 1].[4, 9]gives[9, 4].[7]gives[7].
