Very simple and beat 96%


  • 0
    X

    class MyStack {
    Queue<Integer> queue;

    public MyStack(){
        queue = new LinkedList<Integer>();
    }
    // Push element x onto stack.
    public void push(int x) {
        Queue<Integer> tmp = new LinkedList<Integer>();
        tmp.offer(x);
        for(int i : queue){
            tmp.offer(i);
        }
        queue = tmp;
        
    }
    
    // Removes the element on top of the stack.
    public void pop() {
        queue.poll();
    }
    
    // Get the top element.
    public int top() {
        return queue.peek();
    }
    
    // Return whether the stack is empty.
    public boolean empty() {
        return queue.peek()==null;
    }
    

    }


Log in to reply
 

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