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


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

class CQueue {

    Deque<Integer> stack1;
    Deque<Integer> stack2;

    public CQueue() &#123;
        stack1 = new LinkedList<Integer>();
        stack2 = new LinkedList<Integer>();
    &#125;

    public void appendTail(int value) &#123;
        stack1.push(value);
    &#125;

    public int deleteHead() &#123;
        // 如果第二个栈为空
        if (stack2.isEmpty()) &#123;
            while (!stack1.isEmpty()) &#123;
                stack2.push(stack1.pop());
            &#125;
        &#125;
        if (stack2.isEmpty()) &#123;
            return -1;
        &#125; else &#123;
            int deleteItem = stack2.pop();
            return deleteItem;
        &#125;
    &#125;

// 作者:LeetCode-Solution
// 链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/solution/mian-shi-ti-09-yong-liang-ge-zhan-shi-xian-dui-l-3/
&#125;

文章作者:   future
版权声明:   本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 future !
  目录