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