Note:
先正着存下来,然后再给数组逆序一下即可
代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> ans;
for(auto p = head; p; p = p -> next)
ans.push_back(p -> val);
int l = 0, r = ans.size() - 1;
while(l < r)
swap(ans[l ++], ans[r --]);
return ans;
}
};