剑指 Offer 09. 用两个栈实现队列

// 一种用LinkedList ,一种用Stack  LeetCode 232题
class CQueue {
    private Stack<Integer> s1,s2;
    
    public CQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }
  
    public void appendTail(int value) {
        s1.push(value);
    }
    
    public int deleteHead() {
        if(s2.isEmpty()){
            if(s1.isEmpty()) return -1;

            while(!s1.isEmpty()){
                s2.push(s1.pop());
            }
        }
        return s2.pop();
    }
}


/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */
原文地址:https://www.cnblogs.com/peanut-zh/p/14125489.html