求解代码
Stack<Integer>stack1=newStack<Integer>();Stack<Integer>stack2=newStack<Integer>();publicvoidpush(intnode){stack1.push(node);}publicintpop(){if(stack2.isEmpty()){while(!stack1.isEmpty()){stack2.push(stack1.pop());}}returnstack2.pop();}小贴士
- 所有入队的元素,无条件直接压入
stack1 stack1只管入队,stack2只管出队,并且只有当stack2为空时才进行一次性倒栈