Reverse an array in-place
Java
Easy
4 views
Problem Description
Task: reverse the given int array without using another array. Try using two pointers (start/end) and swap.
Output Format
In-place update
Constraints
Handle empty array and single element.
Official Solution
static void reverseInPlace(int[] a){int l=0,r=a.length-1;while(l<r){int t=a[l];a[l]=a[r];a[r]=t;l++;r--;}}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!