Simple java solution.


  • 0
    J

    Here is Java solution based on two stacks.


    class MyQueue {
        private Deque<Integer> first = new LinkedList<>();
        private Deque<Integer> second = new LinkedList<>();
    
        public void push(int x) {
            first.push(x);
        }
    
        // Removes the element from in front of queue.
        public void pop() {
    
            copyTo();
    
            second.pop();
        }
    
        // Get the front element.
        public int peek() {
    
            copyTo();
    
            return second.peek();
        }
    
        // Return whether the queue is empty.
        public boolean empty() {
    
            return first.isEmpty() && second.isEmpty();
        }
    
        private void copyTo() {
            if(second.isEmpty()) {
                while(!first.isEmpty()) {
                    second.push(first.pop());
                }
            }
        }
    }

Log in to reply
 

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