No2.Add Two Numbers
来源:互联网 发布:机械手臂怎么编程 编辑:程序博客网 时间:2024/04/30 00:32
You are given two linked lists representing two non-negative numbers. 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.
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) {ListNode * result= NULL;ListNode * p1=l1,*p2=l2;ListNode * pNew = NULL, * last = NULL;int pro =0;int val1=0 ,val2=0;while(p1 || p2) { if(p1) { val1 =p1->val; }else val1 =0; if(p2) { val2 =p2->val; }else val2 =0;pNew =new ListNode(0);pNew->val = val1 + val2 + pro;if( 9 < pNew->val ){ pNew->val -=10; pro=1; }elsepro=0;if(!result){result = pNew;last = pNew;}else{last->next = pNew;last = pNew;}if(p1)p1 = p1->next;if(p2)p2= p2->next;}if(pro > 0){ pNew =new ListNode(pro); last->next = pNew;}return result; }};
0 0
- No2.Add Two Numbers
- LeetCode : No2 Add Two Numbers
- leetcode No2. Add Two Numbers
- LEETCODE Problem No2 Add Two Numbers
- 【LeetCode从零单排(Java)】No2. Add Two Numbers
- Add two numbers & Add binary
- LeetCode: Add Two Numbers
- Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode4: Add Two Numbers
- Leetcode : Add Two Numbers
- hdu 1712 ACboy needs your help
- DAO设计模式
- Mahout学习四--贝叶斯分类
- DOM对象
- UVA 120 Stacks of Flapjacks
- No2.Add Two Numbers
- hdu 5312 Sequence【数学推导】
- system函数
- Android Gradle Plugin指南(五)——Build Variants(构建变种版本)
- NSDate类目-实现 1分钟前 X小时前 昨天的显示
- HTML5简单进度条插件
- 静态网站搭建小练习(界面一)
- (_)snprintf跨平台测试
- [dp之路]7.25test