Java solution use two linkedList


  • -1
    O

    class MyStack {

    private Queue<Integer> Myqueue = new LinkedList<Integer>();
    private LinkedList<Integer> linkedList = new LinkedList<Integer>();
    
    public void push(int x) {
    	linkedList.addFirst(x);
    	Myqueue.clear();
    	Myqueue.addAll(linkedList);
    }
    
    // Removes the element on top of the stack.
    public void pop() {
    	Myqueue.remove();
    }
    
    // Get the top element.
    public int top() {
    	return Myqueue.peek();
    }
    
    // Return whether the stack is empty.
    public boolean empty() {
    	return Myqueue.isEmpty();
    }
    

    }


Log in to reply
 

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