public class MinStack {
private StackNode node;
/** initialize your data structure here. */
public MinStack() {
}
public void push(int x) {
StackNode head = new StackNode(x);
head.min = x;
head.next = node;
if(node != null) {
head.min = Math.min(x, node.min);
}
node = head;
}
public void pop() {
node = node.next;
}
public int top() {
return node.val;
}
public int getMin() {
return node.min;
}
//customize minStack
class StackNode {
Integer val;
Integer min;
StackNode next;
public StackNode(){}
public StackNode(Integer val) {
this.val = val;
}
}
}
OOP solution, extremely consice


@strmrdr I tried again, it's not beating 99% any more. Don't know what's going on. Sorry for the misunderstanding.