从尾到头打印链表

题目:输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

 

这道题也很简单,定义一个数组,将链表从头到尾放入数组,最后返回的时候,从尾到头返回即可。

c++代码如下:

 1 class Solution {
 2 public:
 3     vector<int> printListFromTailToHead(ListNode* head) {
 4         vector<int> res;
 5         while(head){
 6             res.push_back(head->val);
 7             head = head->next;
 8         }
 9         return vector<int>(res.rbegin(), res.rend());
10     }
11 };
原文地址:https://www.cnblogs.com/hellosnow/p/12076576.html