> For the complete documentation index, see [llms.txt](https://private-26.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://private-26.gitbook.io/notes/algorithmic-pattern/stack/reversing-an-array.md).

# Reversing an Array

Just push each item onto the stack and then pop it back off.  Because of the stack's LIFO nature, the items come back out in reverse order.

```java
int[] reverserArray(int[] arr) {
    Stack<Integer> stack = new Stack<>();
    
    for(int i = 0; i < arr.length; ++i) {
        stack.push(arr[i]);    
    }
    
    for(int i = 0; !stack.isEmpty(); ++i) {
        arr[i] = stack.pop();
    }
    
    return arr;
}
```

**Runtime :** we do two iteration over the array$$\text{O}(n)$$
