相当于俩个杯子倒腾一下,添加时往一个栈里添加即可。
弹出时,先全部弹出到第二个栈,第二个栈弹出一个作为返回值,再全部弹回去:
import java.util.Stack;
public class Solution {
Stack<integer> stack1 = new Stack<integer>();
Stack<integer> stack2 = new Stack<integer>();</integer></integer></integer></integer>
public void push(int node) {
stack1.push(node);
}
public int pop() {
while(stack1.size() > 0 ){
stack2.push(stack1.pop());
}
int result = stack2.pop();
while(stack2.size() > 0){
stack1.push(stack2.pop());
}
return result;
}}



京公网安备 11010502036488号