Problem
Return a copy of the list with its first and last elements swapped. Lists with fewer than two elements stay the same.
Implement swap_first_last(items).
Input
items: The list of integers.
Output
Return the list with the ends swapped.
Einschränkungen
0 ≤ length ≤ 100,000
Beispiele
Beispiel 1
Eingabe:
items = [1, 2, 3, 4]
Erwartete Ausgabe:
expected = [4, 2, 3, 1]
Eingabe:
std::vector<long long> items = {1, 2, 3, 4};
Erwartete Ausgabe:
std::vector<long long> expected = {4, 2, 3, 1};
Eingabe:
long[] items = new long[] {1, 2, 3, 4};
Erwartete Ausgabe:
long[] expected = new long[] {4, 2, 3, 1};
Erläuterung
1 and 4 trade places: [4, 2, 3, 1].
Beispiel 2
Eingabe:
items = [9]
Erwartete Ausgabe:
expected = [9]
Eingabe:
std::vector<long long> items = {9};
Erwartete Ausgabe:
std::vector<long long> expected = {9};
Eingabe:
long[] items = new long[] {9};
Erwartete Ausgabe:
long[] expected = new long[] {9};
Erläuterung
A single item has nothing to swap with: [9].
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.