輸入一個鏈表的頭節點,從尾到頭反過來返回每個節點的值(用數組返回)。
示例 1:
輸入:head = [1,3,2]
輸出:[2,3,1]
限制:
0 <= 鏈表長度 <= 10000
//翻轉vector
template<typename T>
vector<T> frev(vector<T> v)
{
vector<T> ans;
for(int i=v.size()-1;i>=0;i--)ans.push_back(v[i]);
return ans;
}
class Solution {
public:
vector<int> reversePrint(ListNode * head) {
vector<int>ans;
while(head)
{
ans.push_back(head->val);
head=head->next;
}
return frev(ans);
}
};