Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Reverse an array in-place

Reverse the elements of an array in-place using constant extra space.

Python practice6 minArrays – Fundamentals & PatternsBeginnerLast updated March 25, 2026

Problem statement

Given a list arr, reverse its elements in-place (do not allocate another list). Return the same list after reversal. Your solution should use O(1) extra space and O(n) time, where n is the length of the list.

Task

Implement an in-place reversal of a list and return the modified list.

Examples

Basic example

Input

reverse_array([1, 2, 3])

Output

[3, 2, 1]

The array is reversed in-place and returned.

Input format

A single list of integers, e.g. [1, 2, 3].

Output format

The same list reversed in-place and returned (list).

Constraints

- 0 <= len(arr) <= 10^5 - Elements can be any integers - Must reverse using O(1) extra space

Samples

Sample 1

Input

reverse_array([1, 2, 3, 4])

Output

[4, 3, 2, 1]

Even-length list reversed in-place.