时间限制:1秒 空间限制:32768K 热度指数:685773
本题知识点: 链表
题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
感觉考研考得有点中毒,第一反应竟然想用头插法建个新链表(好麻烦)。。。。。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int>res;
stack<int>ans;
ListNode *p=head;
while(p!=NULL){
ans.push(p->val);
p=p->next;
}
while(!ans.empty()){
int t=ans.top();
ans.pop();
res.push_back(t);
}
return res;
}
};