Problem overview
The method SwapFirstAndLast receives a List<int> named nums, swaps its first and last numbers, and returns it. For [1, 2, 3, 4, 5] the result is [5, 2, 3, 4, 1]: 1 and 5 trade places and the numbers in between do not move. For [4, 9] the result is [9, 4]. For [7] the result is [7], because with one number the first and the last are the same item.
The method changes nums itself and then returns it. It does not build a new list.
Approach: a temporary variable
Two values cannot trade places in one move. If you write the last number into the first place, the old first number is overwritten and lost. So one value has to wait somewhere while the other moves, like a third cup when you swap the drinks in two cups. That place is a temporary variable, here temp.
Step by step
- Save the first number.
int temp = nums[0];keeps its value safe. - Copy the last number into the first place. The last index of a list is
nums.Count - 1: aListhasCount, notLength, and indexes start at 0. Sonums[0] = nums[nums.Count - 1];. Now both ends hold the old last number. - Put the saved number in the last place.
nums[nums.Count - 1] = temp;. The old first number is back, at the other end. - Return the list. It was changed in place, so it comes back swapped.
Here is how the list [1, 2, 3, 4, 5] changes after each assignment:
| After | nums | temp |
|---|---|---|
| start | 1, 2, 3, 4, 5 | unset |
temp = nums[0] | 1, 2, 3, 4, 5 | 1 |
nums[0] = ... | 5, 2, 3, 4, 5 | 1 |
nums[Count - 1] = temp | 5, 2, 3, 4, 1 | 1 |
The solution in C#
class Solution {
public List<int> SwapFirstAndLast(List<int> nums) {
int temp = nums[0];
nums[0] = nums[nums.Count - 1];
nums[nums.Count - 1] = temp;
return nums;
}
}
C# details that matter here
A list is a reference. The method receives the caller's list, not a copy. Changing nums[0] changes the list the caller passed in, and the returned value is that very same list object. This is why the lesson says "change nums itself, then return it".
Count, not Length. Arrays have Length, but List<T> has Count. Writing nums.Length on a list does not compile: the compiler reports that List<int> has no definition for Length.
A shorter swap. C# can swap with a tuple assignment, which does the waiting for you. It gives the same list as the longer version:
(nums[0], nums[nums.Count - 1]) = (nums[nums.Count - 1], nums[0]);
The temp variable version is the one to learn first, because it shows what a swap really needs.
Edge cases and common mistakes
- Skipping the temp. Doing
nums[0] = nums[nums.Count - 1];and thennums[nums.Count - 1] = nums[0];copies the same number to both ends, becausenums[0]was already overwritten. - One item. The first and last index are both 0. The three assignments save the number, write it to the same place, and write it again, so the list is unchanged.
- Two items.
[4, 9]becomes[9, 4]: the two items are the first and the last. - Using
nums.Countas the index. The last index isCount - 1. An index equal toCountthrows anArgumentOutOfRangeException. The same exception comes from an empty list, but the lesson promises at least one number.
Testing
| nums | Result |
|---|---|
[1, 2, 3, 4, 5] | [5, 2, 3, 4, 1] |
[4, 9] | [9, 4] |
[7] | [7] |
