Code Logo

Reverse Like Linked List

Published at25 Jul 2026
Easy 0 views
Like0

Reverse an array of elements. This simulates reversing a linked list where each element points to the next.

Example Input & Output

Example 1
Input
[1,2,3,4,5]
Output
[5,4,3,2,1]
Explanation

Reverse order

Algorithm Flow

Recommendation Algorithm Flow for Reverse Like Linked List
Recommendation Algorithm Flow for Reverse Like Linked List

Solution Approach

function solution(arr) {
  return arr.slice().reverse();
}

Best Answers

java
class Solution {
    public int[] solution(int[] nums) {
        int[] r=nums.clone();
        for(int i=0,j=r.length-1;i<j;i++,j--){int t=r[i];r[i]=r[j];r[j]=t;}
        return r;
    }
}