Using LinkedList based new data structure


  • 0
    X
    class LinkNode{
        int val;
        LinkNode next;
        int min;
        public LinkNode(int value, int minVal)
        {
            this.val = value;
            this.min = minVal;
        }
    }
    
    LinkNode top = new LinkNode(0,0);
    
    public void push(int x) {
        LinkNode node=null;
        if(top.next==null)
             node = new LinkNode(x,x);
        else{
            int min = Math.min(x, top.next.min);
            node = new LinkNode(x,min);
        }
        node.next = top.next;
        top.next = node;
    }
    
    public void pop() {
        if(top.next!=null)
            top.next = top.next.next;
    }
    
    public int top() {
        return top.next.val;
    }
    
    public int getMin() {
        return top.next.min;
    }

Log in to reply
 

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