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