栈实现从头打印链表
从尾到头打印链表
http://www.nowcoder.com/questionTerminal/d0267f7f55b3412ba93bd35cfa8e8035
C++使用栈
/**
- struct ListNode {
- int val;
- struct ListNode *next;
- ListNode(int x) :
- val(x), next(NULL) {
- }
- };
- /
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) { //除了官方的三种方法外,第四种用栈的方法stack<int> temp; while(head){ temp.push(head->val); head = head->next; } vector<int> ret; while(!temp.empty()){ ret.push_back(temp.top()); temp.pop(); } return ret;
}
};
```</int>