国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 海伦市| 莱西市| 牙克石市| 长沙县| 吉首市| 岳池县| 利津县| 南岸区| 杭锦旗| 长顺县| 满城县| 宁海县| 荣昌县| 察隅县| 镇赉县| 图们市| 新昌县| 扎兰屯市| 香港| 上虞市| 贵南县| 富平县| 宁安市| 从江县| 鄂州市| 花莲县| 鸡西市| 惠安县| 化州市| 大城县| 沛县| 垦利县| 沈阳市| 江源县| 曲周县| 北海市| 芜湖市| 喀喇| 行唐县| 内乡县| 宜章县|