155. Min Stack

class MinStack {
    private static class Node {
        int value;
        int min;
        Node next;
        Node(int value, int min) {
            this.value = value;
            this.min = min;
            this.next = null;
        }
    }

    private Node head;

    public MinStack() {
        this.head = new Node(42, 42);
    }

    public void push(int x) {
        if (head.next == null) {
            head.next = new Node(x, x);
        } else {
            Node oldNext = head.next;
            // the top node always keeps the minimum
            head.next = new Node(x, Math.min(x, oldNext.min));
            head.next.next = oldNext;
        }
    }

    public void pop() {
        Node nextNext = head.next.next;
        head.next.next = null;
        head.next = nextNext;
    }

    public int top() {
        return head.next.value;
    }

    public int getMin() {
        return head.next.min;
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */