import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.add(node);
while(stack2.isEmpty()){
while(!stack1.isEmpty()){
stack2.add(stack1.pop());
}
}
}
public int pop() {
if(!stack2.isEmpty()){
return stack2.pop();
}else{
while(!stack1.isEmpty()){
stack2.add(stack1.pop());
}
}
if(stack2.isEmpty()){
return -1;
}
return stack2.pop();
}
} 第一个栈保存进栈数据,如果第二栈为空的时候则将栈一的数据压入栈二,出栈的时候如果栈二有数据出栈二的数据,如果没有数据先将栈一数据压入栈二在进行出栈

京公网安备 11010502036488号