剑指 Offer 09. 用两个栈实现队列
class CQueue {
private Stack<Integer> sta1;
private Stack<Integer> sta2;
public CQueue() {
sta1 = new Stack<>();
sta2 = new Stack<>();
}
public void appendTail(int value) {
while(!sta1.empty()){
sta2.push(sta1.pop());
}
sta1.push(value);
while(!sta2.empty()){
sta1.push(sta2.pop());
}
}
public int deleteHead() {
if(sta1.empty())
return -1;
else return sta1.pop();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/