It's really easy by using LinkedList , Java


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

  • 0
    B

    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.