Accepted clean Java solution


  • 25
      class MyStack {
        Queue<Integer> q = new LinkedList<Integer>();
        
        // Push element x onto stack.
        public void push(int x) {
            q.add(x);
            
            int n = q.size();
            while (n-- > 1)
                q.add(q.poll());
        }
    
        // Removes the element on top of the stack.
        public void pop() {
            q.poll();
        }
    
        // Get the top element.
        public int top() {
            return q.peek();
        }
    
        // Return whether the stack is empty.
        public boolean empty() {
            return q.isEmpty();
        }
    
      }

  • 0
    M

    your method is fast !! I like your method, you always come up with good solutions !


Log in to reply
 

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