题目描述
输入一个链表,从尾到头打印链表每个节点的值。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> temp;
stack<struct ListNode *> nodes;
ListNode* thead = head;
while(thead)
{
nodes.push(thead);
thead = thead->next;
}
while(!nodes.empty())
{
thead = nodes.top();
temp.push_back(thead->val);
nodes.pop();
}
return temp;
}
};
07-28
07-28
07-28
07-28
07-28
07-28