/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> arr;
vector<int> arr1;
int i,length;
while(head!=NULL){
arr.push_back(head->val);
head=head->next;
}
length=arr.size();
for(i=length-1;i>=0;i--){
arr1.push_back(arr[i]);
}
return arr1;
}
};
剑指 offer-3-从头到尾打印链表-c++
最新推荐文章于 2024-08-04 13:58:13 发布
本文介绍了一种使用C++实现的算法,该算法能够将链表中的元素逆序输出。通过两次遍历链表的方式,首先收集链表的所有元素值,然后逆序输出这些值,实现了从尾到头的打印过程。
2205

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



