从尾到头打印链表

xiaoxiao2021-02-28  25

时间限制: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()); //反向迭代器 } };
转载请注明原文地址: https://www.6miu.com/read-2600269.html

最新回复(0)