Add Two Numbers
来源:互联网 发布:sql删除表数据语句 编辑:程序博客网 时间:2024/06/06 15:38
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.
/** * 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 headNode(INT_MAX); ListNode* head = &headNode; int carry = 0; while(l1 || l2) { int val1 = l1?l1->val:0; int val2 = l2?l2->val:0; int tmp = val1+val2+carry; head->next = new ListNode(tmp%10); carry = tmp/10; head = head->next; if(l1) l1 = l1->next; if(l2) l2 = l2->next; } if(carry!=0) { head->next = new ListNode(carry); } return headNode.next; }};
0 0
- 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
- Add Two Numbers
- 【leetcode】Add Two Numbers
- add two numbers
- Add Two Numbers
- Add Two Numbers
- Codeforces-665E-Beautiful Subarrays Trie树(字典树)
- Java 泛型(一) 泛型使用基础
- Java设计模式之工厂方法模式【Factory Method Pattern】
- Android中ListView内容超过一屏,上下滑动后数据紊乱的问题
- check the manual that corresponds to your MySQL server version for the right syntax to use near 'USI
- Add Two Numbers
- matlab2014a中help与参考页翻译 and
- ReactNative真机调试
- 【学习C++】3.构造函数
- awk相关
- HDU 1397 Goldbach's Conjecture(素数判断)
- win8/Metro开发系列一 Xaml布局
- Java 泛型(二) 泛型之中的通配符(Wildcards)使用
- NYOJ 38 布线问题