力扣-Java用栈实现队列

力扣-Java用栈实现队列_第1张图片

题解:

class MyQueue {
        // 用deque开挂也方便
//    private Deque queue = null;
    private List queue;

    public MyQueue() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.add(x);
    }

    public int pop() {
        return queue.remove(0);
    }

    public int peek() {
        return queue.get(0);
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}

 

你可能感兴趣的:(java,leetcode,算法)