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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

Lintcode 167. 链表求和 221. 链表求和 II 题解

發(fā)布時(shí)間:2025/7/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lintcode 167. 链表求和 221. 链表求和 II 题解 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

167. 鏈表求和

描述

你有兩個(gè)用鏈表代表的整數(shù),其中每個(gè)節(jié)點(diǎn)包含一個(gè)數(shù)字。數(shù)字存儲(chǔ)按照在原來(lái)整數(shù)中相反的順序,使得第一個(gè)數(shù)字位于鏈表的開(kāi)頭。寫(xiě)出一個(gè)函數(shù)將兩個(gè)整數(shù)相加,用鏈表形式返回和。

樣例

給出兩個(gè)鏈表?3->1->5->null?和?5->9->2->null,返回?8->0->8->null

思路:從左往右相加,考慮進(jìn)位即可。一些細(xì)節(jié)一定要自己寫(xiě)一遍才能體會(huì)

/*** Definition of singly-linked-list:* class ListNode {* public:* int val;* ListNode *next;* ListNode(int val) {* this->val = val;* this->next = NULL;* }* }*/class Solution { public:/*** @param l1: the first list* @param l2: the second list* @return: the sum list of l1 and l2 */ListNode * addLists(ListNode * l1, ListNode * l2) {// write your code hereListNode *cur1 = l1;ListNode *cur2 = l2;ListNode *res = new ListNode(-1);ListNode *res_cur = res;int flag = 0;while(cur1 || cur2){int sum = (cur1?cur1->val:0) + (cur2?cur2->val:0) + flag;ListNode *temp = new ListNode(sum%10);res_cur -> next = temp;res_cur = res_cur->next;flag = sum/10;cur1 = cur1?cur1->next:cur1;cur2 = cur2?cur2->next:cur2;}if(flag) res_cur->next = new ListNode(1);//最后的進(jìn)位,易錯(cuò)點(diǎn)return res->next;} };

?

?

?

221.?鏈表求和 II

描述

假定用一個(gè)鏈表表示兩個(gè)數(shù),其中每個(gè)節(jié)點(diǎn)僅包含一個(gè)數(shù)字。假設(shè)這兩個(gè)數(shù)的數(shù)字順序排列,請(qǐng)?jiān)O(shè)計(jì)一種方法將兩個(gè)數(shù)相加,并將其結(jié)果表現(xiàn)為鏈表的形式。

樣例

給出?6->1->7?+?2->9->5。即,617 + 295。

返回?9->1->2。即,912?。

思路:是上面題目的進(jìn)階版,數(shù)字順序變了,自然考慮到翻轉(zhuǎn)鏈表再計(jì)算。

/*** Definition of singly-linked-list:* class ListNode {* public:* int val;* ListNode *next;* ListNode(int val) {* this->val = val;* this->next = NULL;* }* }*/class Solution { public:/*** @param l1: The first list.* @param l2: The second list.* @return: the sum list of l1 and l2.*/ListNode * addLists2(ListNode * l1, ListNode * l2) {// write your code hereListNode *l1_rev = reverseList(l1);ListNode *l2_rev = reverseList(l2);return reverseList(addLists(l1_rev,l2_rev));//最后別忘記翻回來(lái)}ListNode * reverseList(ListNode *l){    //翻轉(zhuǎn)鏈表ListNode *res = new ListNode(-1);ListNode *cur = l;while(cur){ListNode * temp = cur->next;cur->next = res->next;res->next = cur;cur = temp;}return res->next;}ListNode * addLists(ListNode * l1, ListNode * l2) {//翻轉(zhuǎn)后的鏈表求和,同上// write your code hereListNode *cur1 = l1;ListNode *cur2 = l2;ListNode *res = new ListNode(-1);ListNode *res_cur = res;int flag = 0;while(cur1 || cur2){int sum = (cur1?cur1->val:0) + (cur2?cur2->val:0) + flag;ListNode *temp = new ListNode(sum%10);res_cur -> next = temp;res_cur = res_cur->next;flag = sum/10;cur1 = cur1?cur1->next:cur1;cur2 = cur2?cur2->next:cur2;}if(flag) res_cur->next = new ListNode(1);return res->next;} };

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/J1ac/p/9092733.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Lintcode 167. 链表求和 221. 链表求和 II 题解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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