समस्या
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.
सीमाएँ
0 ≤ length ≤ 100,000
उदाहरण
उदाहरण 1
इनपुट:
items = [1, 2, 3, 4]
अपेक्षित आउटपुट:
expected = [4, 2, 3, 1]
इनपुट:
std::vector<long long> items = {1, 2, 3, 4};
अपेक्षित आउटपुट:
std::vector<long long> expected = {4, 2, 3, 1};
इनपुट:
long[] items = new long[] {1, 2, 3, 4};
अपेक्षित आउटपुट:
long[] expected = new long[] {4, 2, 3, 1};
व्याख्या
1 and 4 trade places: [4, 2, 3, 1].
उदाहरण 2
इनपुट:
items = [9]
अपेक्षित आउटपुट:
expected = [9]
इनपुट:
std::vector<long long> items = {9};
अपेक्षित आउटपुट:
std::vector<long long> expected = {9};
इनपुट:
long[] items = new long[] {9};
अपेक्षित आउटपुट:
long[] expected = new long[] {9};
व्याख्या
A single item has nothing to swap with: [9].
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।