时间限制:1秒空间限制:32768K
算法知识视频讲解
输入一个链表,从尾到头打印链表每个节点的值。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector <int> a;
ListNode *t = head;
//t = (ListNode)malloc(sizeof(ListNode));
//t = (head)->next;
while(t != nullptr)
{
a.push_back(t ->val);
t = t->next;
}
return vector<int>(a.rbegin(), a.rend()); //反向迭代器
}
};