Start learning free

LESSON 116 OF 289 · ARRAYS - CHALLENGES

Swap First and Last in C#: Swapping List Items with a Temp Variable

Practice Read the full explanation

Given a List 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:

  • nums has at least one number.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 4
    Ctrl + Enter to check

    C# · ARRAYS - CHALLENGES

    Swap First And Last in C#, explained

    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

    1. Save the first number. int temp = nums[0]; keeps its value safe.
    2. Copy the last number into the first place. The last index of a list is nums.Count - 1: a List has Count, not Length, and indexes start at 0. So nums[0] = nums[nums.Count - 1];. Now both ends hold the old last number.
    3. Put the saved number in the last place. nums[nums.Count - 1] = temp;. The old first number is back, at the other end.
    4. 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:

    Afternumstemp
    start1, 2, 3, 4, 5unset
    temp = nums[0]1, 2, 3, 4, 51
    nums[0] = ...5, 2, 3, 4, 51
    nums[Count - 1] = temp5, 2, 3, 4, 11

    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 then nums[nums.Count - 1] = nums[0]; copies the same number to both ends, because nums[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.Count as the index. The last index is Count - 1. An index equal to Count throws an ArgumentOutOfRangeException. The same exception comes from an empty list, but the lesson promises at least one number.

    Testing

    numsResult
    [1, 2, 3, 4, 5][5, 2, 3, 4, 1]
    [4, 9][9, 4]
    [7][7]
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free