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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LeetCode 2. Add Two Numbers

2019-11-08 02:02:56
字體:
供稿:網(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. Examples:

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

程序代碼:

/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode *p,*q,*r,*m,*tmp; p=l1,q=l2; int sum=0,flag = 0; r = (struct ListNode*)malloc(sizeof(struct ListNode)); m=r; while(p!=NULL&&q!=NULL) { tmp = (struct ListNode*)malloc(sizeof(struct ListNode)); sum = p->val + q->val + flag; flag=sum>=10?1:0; tmp->val = sum%10; tmp->next = NULL; m->next = tmp; m= m->next; p=p->next; q=q->next; } while(p!=NULL) { tmp = (struct ListNode*)malloc(sizeof(struct ListNode)); sum = p->val + flag; flag=sum>=10?1:0; tmp->val = sum%10; tmp->next = NULL; m->next = tmp; m= m->next; p=p->next; } while(q!=NULL) { tmp = (struct ListNode*)malloc(sizeof(struct ListNode)); sum = q->val + flag; flag=sum>=10?1:0; tmp->val = sum%10; tmp->next = NULL; m->next = tmp; m= m->next; q=q->next; } if(flag) { tmp = (struct ListNode*)malloc(sizeof(struct ListNode)); tmp->val = 1; tmp->next = NULL; m->next = tmp; m=m->next; } return r->next;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 两当县| 无为县| 肥西县| 商都县| 鹿泉市| 宁陵县| 分宜县| 呼玛县| 焦作市| 岑巩县| 二手房| 包头市| 荃湾区| 夏河县| 孟连| 洱源县| 晋中市| 万年县| 闽侯县| 连云港市| 礼泉县| 城市| 永川市| 南宫市| 探索| 梓潼县| 巩留县| 福建省| 伽师县| 嫩江县| 淮北市| 永定县| 宜宾县| 岗巴县| 温泉县| 鄂州市| 舟山市| 荣昌县| 花莲县| 上饶市| 忻城县|