时间限制:1秒 空间限制:32768K 热度指数:741521
本题知识点: 链表
算法知识视频讲解
题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
stack<int> s;
vector<int> a;
while(head){
s.push(head->val);
head=head->next;
}
while(!s.empty()){
a.push_back(s.top());
s.pop();
}
return a;
}
};