Problem overview
The method swapFirstAndLast receives a list of integers, nums, swaps its first and last numbers, and returns it. The change is made on nums itself, and the numbers in between stay where they are. A list of one number is returned as it is, since its first and last numbers are the same item. Every list has at least one number.
For [1, 2, 3, 4, 5] the result is [5, 2, 3, 4, 1]. For [4, 9] it is [9, 4]. For [7] it is [7].
Approach: park one value in a temporary variable
A swap can't be done in one move. If you first copy the last number into position 0, the original first number is overwritten and lost. So one value has to wait somewhere, like a third cup when you swap the drinks in two cups. That place is a temporary variable, temp.
The order of the three moves is:
- save the first number in
temp; - copy the last number into the first place;
- write
tempinto the last place.
For lists, two methods do all the work. get(i) reads the item at index i. set(i, value) replaces the item at index i and doesn't change the size. Indexes start at 0, so the first item is at 0 and the last is at size() - 1.
Step by step
- Save the first number.
int temp = nums.get(0);holds the value for a moment. - Overwrite the first place with the last number.
nums.set(0, nums.get(nums.size() - 1));reads the last item and stores it at index 0. - Write the saved number into the last place.
nums.set(nums.size() - 1, temp);puts the old first number at the end. Index 0 already holds the last number, so the old first number survives only intemp. - Return the list. The list itself was changed, so returning
numsreturns it swapped.
The solution in Java
class Solution {
public List<Integer> swapFirstAndLast(List<Integer> nums) {
int temp = nums.get(0);
nums.set(0, nums.get(nums.size() - 1));
nums.set(nums.size() - 1, temp);
return nums;
}
}
For the three examples, with nums built as an ArrayList, the calls print:
[5, 2, 3, 4, 1]
[9, 4]
[7]
Some Java details matter here:
- The element type.
List<Integer>is a list ofIntegerobjects.nums.get(0)returns anInteger, which Java unboxes automatically into theint temp, andsetboxes the values back. - The list is modified in place. Lists are objects, and the method receives a reference to the caller's list. After the call, the caller's list is swapped too. The returned value is the same object.
- Only
setreplaces items.addwould insert a new item and grow the list. - Not every list can be changed. A list from
List.of(1, 2, 3)is unmodifiable, and callingseton it throws anUnsupportedOperationException. A list fromnew ArrayList<>(List.of(1, 2, 3))can be changed.
Edge cases and common mistakes
- Skipping the temporary variable. Setting index 0 to the last number and then index
size() - 1tonums.get(0)writes the last number back to itself, so[1, 2, 3]would become[3, 2, 3]. - One number. With
[7]the first and last indexes are both 0. Save 7, write 7 to index 0, then write 7 to index 0 again: the list is unchanged, with no special case. - Two numbers.
[4, 9]works with the same three moves and becomes[9, 4]. - Using
size()as an index. The last valid index issize() - 1. Indexsize()throws anIndexOutOfBoundsException. - Forgetting the return. The list is already swapped, but the method must still hand it back.
- Square brackets.
nums[0]is for arrays. A list needsget(0).
Testing
[1, 2, 3, 4, 5]gives[5, 2, 3, 4, 1].[4, 9]gives[9, 4].[7]gives[7].
