-
Notifications
You must be signed in to change notification settings - Fork 19.5k
/
ReverseStackUsingRecursion.java
44 lines (40 loc) · 1.24 KB
/
ReverseStackUsingRecursion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.thealgorithms.others;
import java.util.Stack;
/**
* Class that provides methods to reverse a stack using recursion.
*/
public final class ReverseStackUsingRecursion {
private ReverseStackUsingRecursion() {
}
/**
* Reverses the elements of the given stack using recursion.
*
* @param stack the stack to be reversed
* @throws IllegalArgumentException if the stack is null
*/
public static void reverse(Stack<Integer> stack) {
if (stack == null) {
throw new IllegalArgumentException("Stack cannot be null");
}
if (!stack.isEmpty()) {
int topElement = stack.pop();
reverse(stack);
insertAtBottom(stack, topElement);
}
}
/**
* Inserts an element at the bottom of the given stack.
*
* @param stack the stack where the element will be inserted
* @param element the element to be inserted at the bottom
*/
private static void insertAtBottom(Stack<Integer> stack, int element) {
if (stack.isEmpty()) {
stack.push(element);
} else {
int topElement = stack.pop();
insertAtBottom(stack, element);
stack.push(topElement);
}
}
}