-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path155_gp.cpp
54 lines (44 loc) · 1.21 KB
/
155_gp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class MinStack {
public:
/** initialize your data structure here. */
struct ListNode{
int val;
int minval; // key record
ListNode* next;
ListNode(int val, int minval): val(val), next(NULL), minval(minval){
}
};
MinStack() {
this->m_header = new ListNode(INT_MAX, INT_MAX);
}
~MinStack() {
}
void push(int x) {
int newmin = this->m_header->next == NULL ? x : min(x, this->m_header->next->minval);
ListNode* newNode = new ListNode(x, newmin);
ListNode* tmp = this->m_header->next;
this->m_header->next = newNode;
newNode->next = tmp;
}
void pop() {
ListNode* tmp = this->m_header->next;
this->m_header->next = this->m_header->next->next;
delete tmp;
}
int top() {
return this->m_header->next->val;
}
int getMin() {
return this->m_header->next->minval;
}
private:
ListNode* m_header;
};
/**
* 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();
*/