题解 | #从尾到头打印链表#
从尾到头打印链表
http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> ans;
int i=0;
while(head)
{
ans.push_back(head->val);
head=head->next;
}
reverse(ans.begin(),ans.end());
return ans;
}
}; 

查看7道真题和解析