1. 正常两个栈
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
2. 栈+递归栈
public class Solution {
Stack<Integer> stack = new Stack<Integer>();
public void push(int node) {
stack.push(node);
}
public int pop() {
if (stack.size() == 1) {
return stack.pop();
}
int temp = stack.pop();
int ret = pop();
stack.push(temp);
return ret;
}
}