国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

2. Add Two Numbers

2019-11-06 08:21:10
字體:
供稿:網(wǎng)友

You are given two non-empty linked lists rePResenting two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8

不難的題

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int c=0;//存儲進位 ListNode* re=l1; while(l1->next!=NULL && l2->next!=NULL) { int temp=(l1->val+l2->val+c)/10; l1->val=(l1->val+l2->val+c)%10; c=temp; l1=l1->next; l2=l2->next; } if(l1->next==NULL)//l1短的情況 { l1->val+=l2->val; l1->next=l2->next; } else l1->val+=l2->val; while(l1->next!=NULL) { int temp=(l1->val+c)/10; l1->val=(l1->val+c)%10; c=temp; l1=l1->next; } if((c+l1->val)/10==0) l1->val+=c; else{ l1->val=(c+l1->val)%10; ListNode te(1); l1->next=&te; } return re; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉义市| 根河市| 大埔县| 随州市| 漠河县| 塘沽区| 天等县| 隆安县| 右玉县| 启东市| 马边| 凤城市| 沁阳市| 武陟县| 桐乡市| 英吉沙县| 读书| 小金县| 周宁县| 武安市| 鹤峰县| 南召县| 湘潭县| 双流县| 二手房| 栾城县| 商都县| 英吉沙县| 出国| 五河县| 新巴尔虎左旗| 古交市| 桂东县| 西贡区| 当雄县| 临泉县| 安溪县| 荥经县| 扶绥县| 莱西市| 大厂|