C++代码如下:
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> v1 = vector<int>();
if (head == nullptr) {
return v1;
}
ListNode* list = head;
while (list != nullptr) {
v1.insert(v1.begin(), list->val);
list = list->next;
}
return v1;
}
};
运行结果: