My Java solution using only one Queue


  • 0
    B

    class MyStack {

    Queue<Integer> queue=new LinkedList<Integer>();
    public void push(int x) {
    	queue.offer(x);
    	int i=queue.size()-1;
    	while(i>0)
    	{
    		queue.offer(queue.poll());
    		i--;
    	}
        
    }
    
    
    public void pop() {
        queue.poll();
    }
    
    
    public int top() {
        return queue.peek();
    }
    
    
    public boolean empty() {
    	return queue.isEmpty();
        
    }
    

    }


Log in to reply
 

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