【second】Add Two Numbers
2013-10-22 13:40
134 查看
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { // Start typing your C/C++ solution below // DO NOT write int main() function int carry = 0; ListNode* nhead=NULL,*prev=NULL; ListNode* it1=l1,*it2=l2; while(it1||it2||carry) { int num1 = (it1?it1->val:0); int num2 = (it2?it2->val:0); int sum = num1+num2+carry; if(nhead==NULL) prev = nhead = new ListNode(sum%10); else { prev->next = new ListNode(sum%10); prev = prev->next; } carry = sum/10; it1 = (it1?it1->next:NULL); it2 = (it2?it2->next:NULL); } return nhead; }
相关文章推荐
- LeetCode-Algorithms #002 Add Two Numbers, Database #176 Second Highest Salary
- 【Leetcode问题搬运翻译】Add two numbers
- Java [leetcode 2] Add Two Numbers
- Leetcode Add Two Numbers
- [leetCode]Add Two Numbers
- 2. Add Two Numbers
- 2-m-Add Two Numbers
- Add Two Numbers
- LeetCode: Add Two Numbers
- LeetCode 002 AddTwoNumbers
- [leetcode] Add Two Numbers
- Add Two Numbers
- 【Leetcode】Add Two Numbers
- LeetCode Add Two Numbers
- LeetCode | Add Two Numbers
- LeetCode-002 Add Two Numbers
- Leetcode [Add Two Numbers]
- week_2_Add Two Numbers
- leetcode 2 Add Two Numbers
- 2. Add Two Numbers