It's really easy by using LinkedList , Java

  • -11
    class MyStack {
            LinkedList<Integer> queue = new LinkedList<Integer>();
            // Push element x onto stack.
            public void push(int x) {
            // Removes the element on top of the stack.
            public void pop() {
            // Get the top element.
            public int top() {
                return queue.peekLast();
            // Return whether the stack is empty.
            public boolean empty() {
                return queue.isEmpty();

  • 0

    standard operations of a queue -- which means only push to back, pop from front, size, and is empty operations are valid

  • 0

    Guess what, it's even easier by using Stack!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.