```
class MinStack {
private Node stackHead = new Node(0);
private Node minHead = new Node(0);
public void push(int x) {
Node tmp;
if (stackHead.next == null) { // stackHead and minHead have same state
stackHead.next = new Node(x);
minHead.next = new Node(x);
}
else {
tmp = new Node(x);
tmp.next = stackHead.next;
stackHead.next = tmp;
tmp = new Node(Math.min(x, minHead.next.val));
tmp.next = minHead.next;
minHead.next = tmp;
}
}
public void pop() {
if (stackHead.next != null) {
stackHead.next = stackHead.next.next;
minHead.next = minHead.next.next;
}
else {
return;
}
}
public int top() {
if (stackHead.next != null) {
return stackHead.next.val;
}
else {
return -1;
}
}
public int getMin() {
if (minHead.next != null) {
return minHead.next.val;
}
else {
return -1;
}
}
}
class Node {
public Node next;
public int val;
public Node(int val) {
this.val = val;
}
}
```