Accepted solution use one stack and one list


  • -2
    O

    class MyQueue {

        Stack<Integer> stack=new Stack<Integer>();
        List<Integer> list=new ArrayList<Integer>();
        // Push element x to the back of queue.
        public void push(int x) {
            list.add(0, x);
            stack.clear();
            stack.addAll(list);
        }
    
        // Removes the element from in front of queue.
        public void pop() {
            list.remove(0);
            stack.pop();
        }
    
        // Get the front element.
        public int peek() {
            return stack.peek();
        }
    
        // Return whether the queue is empty.
        public boolean empty() {
           return stack.empty();
        }
    

    }


Log in to reply
 

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