Java concise easy solution 1 line for each method


  • 0
    public class MinStack {
        Node top = null;
    
        public int getMin() {
            return top.min;
        }   
    
        public void pop() {
            top = top.next;
        }   
    
        public void push(int x) {
            top = new Node(top, x, top == null ? x : Math.min(x, top.min));
        }   
    
        public int top() {
            return top.data;
        }   
    
        static class Node {
            int data, min;
            Node next;
    
            Node(Node next, int data, int min) {
                this.next = next;
                this.data = data;
                this.min = min;
            }   
        }   
    }
    

Log in to reply
 

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