题目描述
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
stack<ListNode*> stk;
ListNode *t;
t = head;
while(t != NULL)
{
stk.push(t);
t = t->next;
}
int size = stk.size();
vector<int> result;
for(int i=0; i<size; ++i)
{
result.push_back(stk.top()->val);
stk.pop();
}
return result;
}
};