LinkedList implementation(Java) 7ms


  • 0
    C

    class MinStack {
    private Node first;

    public void push(int x) {
        Node oldFirst = first;
        int minItem = (oldFirst == null || x <= oldFirst.minItem) ? x : oldFirst.minItem;
        first = new Node(x, minItem);
        first.next = oldFirst;
    }
    
    public void pop() {
        first = first.next;
    }
    
    public int top() {
        return first.item;
    }
    
    public int getMin() {
        return first.minItem;
    }
    
    private class Node{
        private final int item;
        private final int minItem;
        private Node next;
        Node(int item, int minItem){
            this.item = item;
            this.minItem = minItem;
        }
    }
    

    }


Log in to reply
 

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