Lists · Easy

Reverse a list

Return a new list with the elements in reverse order. Leave the input unchanged. The new list starts with the original last element. Reading it backward leaves the original list intact.

Worked example

[1, 2, 3] → [3, 2, 1]

  1. Take 3 from the end.
  2. Append 2, then 1. The new list is [3, 2, 1].

Python solution

def solve(nums):
    return list(reversed(nums))

reversed walks backward through the list. list collects the result without changing the input.

Time: O(n) · Space: O(n)

Practise in GymType