思路1:入队时,如果栈2不空,把栈2元素出到栈1中再入栈;出队时,如果栈1不空,把栈1出到栈2;(略为繁琐)
思路2:入队时,直接入;出队时,如果栈2不空,直接出,如果空,把栈1元素出到栈2.
public void push(int node) {
while(!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
stack1.push(node);
}
public int pop() {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
}