Start learning free

LESSON 117 OF 291 · ARRAYS - CHALLENGES

Swap First and Last in Kotlin: MutableList

Practice Read the full explanation

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:

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

    KOTLIN · ARRAYS - CHALLENGES

    Swap First And Last in Kotlin, explained

    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

    1. Save the first number in a val named temp: nums[0]. The next step overwrites that place, so its old value would be lost otherwise.
    2. Copy the last number into the first place: nums[0] = nums[nums.size - 1]. The last index is the size minus 1.
    3. Put the saved number in the last place: nums[nums.size - 1] = temp.
    4. 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 a holding [1, 2, 3, 4, 5], both the result and a print as [5, 2, 3, 4, 1], and result === a is true.
    • Indexing assignment. nums[0] = value is shorthand for calling set(0, value) on the list.
    • val for the temporary. The saved number is never reassigned, so val fits.
    • 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 then nums[nums.size - 1] = nums[0] copies the last number into both places, because nums[0] was already overwritten.
    • Using nums.size as the last index. It is one past the end and throws an IndexOutOfBoundsException.
    • An empty list. The lesson promises at least one number. On an empty list, nums[0] throws an IndexOutOfBoundsException.
    • Read-only lists. The signature uses MutableList because the function writes to the list. Declaring the parameter as List<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].
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free