算法1
思路:从前往后遍历一下链表,然后把值存入链表里面,然后用reverse()翻转
C++ 代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> printListReversingly(ListNode* head) {
vector<int> res;
for(auto p=head; p;p=p->next) res.push_back(p->val);//从前往后遍历一下链表,然后把值存入链表里面
reverse(res.begin(),res.end());
return res;
}
};