simple java LinkedList solution


  • 0
    Z
    class MinStack {
        LinkedList<Integer> list = null;
        int min = Integer.MAX_VALUE;
        /** initialize your data structure here. */
        public MinStack() {
            list = new LinkedList<Integer>();
        }
        
        public void push(int x) {
            if(x<=min) {
                list.addFirst(min);
                min = x;
            }
            list.addFirst(x);
            
        }
        
        public void pop() {
            int num = list.removeFirst();
            if(num==min) min = list.removeFirst();
        }
        
        public int top() {
            return list.getFirst();
        }
        
        public int getMin() {
            return min;
        }
    }
    

Log in to reply
 

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