题目描述
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
思路
- 栈
- 头插法(双端队列或数组)
code
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> ret;
ListNode *p=head;
while(p!=NULL){
ret.insert(ret.begin(),p->val);
p=p->next;
}
return ret;
}
};