题目:
解决方法:
push 正常
pop 栈2是否为空,不空,正常pop,空,将正常push的栈中的元素pop到stack2中
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.push(node);
}
public int pop() {
int num = 0;
if(stack2.size()==0){
while(stack1.size()>1){
stack2.push(stack1.pop());
}
num = stack1.pop();
}
else
num = stack2.pop();
return num;
}
}