题目描述
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
看到书上有推荐使用递归,或者栈来接收的,这里我使用vector:
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> res;
while(head!=NULL) {
res.push_back(head->val);
head=head->next;
}
reverse(res.begin(),res.end());
return res;
}
};
本文介绍了一种使用vector实现链表逆序输出的方法。通过遍历链表将节点值存入vector,再反转vector实现从尾到头的顺序输出。
1278

被折叠的 条评论
为什么被折叠?



