日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

06. 从尾到头打印链表

發(fā)布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 06. 从尾到头打印链表 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2020-06-22

1.題目描述

輸入一個鏈表的頭節(jié)點,從尾到頭反過來返回每個節(jié)點的值(用數(shù)組返回)。

2.題解

1.放入vector中,然后reverse 2.將鏈表反轉(zhuǎn)

3.代碼

/*** 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>tmp;ListNode* p=head;while (p){tmp.push_back(p->val);p=p->next;}reverse(tmp.begin(),tmp.end());return tmp;} }; /*** 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) {if (!head) return vector<int>(0);vector<int>tmp;ListNode* pre=head,*p=pre->next,*t;pre->next=NULL;while (p){t=p->next;p->next=pre;pre=p;p=t;}while (pre){tmp.push_back(pre->val);pre=pre->next;}return tmp;} };

總結(jié)

以上是生活随笔為你收集整理的06. 从尾到头打印链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。