leetcode Add Two Numbers
来源:互联网 发布:赢在中国知乎 编辑:程序博客网 时间:2024/05/15 18:10
/** * 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) { int carry = 0; ListNode* tmpl1 = l1, *tmpl2 = l2; ListNode dummy(-1); ListNode* tmprec = &dummy; // cout << "hello" << endl; //return NULL; while(tmpl1 != NULL && tmpl2 != NULL){ int res = carry + tmpl1->val + tmpl2->val; carry = res / 10; res %= 10; tmpl1 = tmpl1->next; tmpl2 = tmpl2->next; tmprec->next = new ListNode(res); tmprec = tmprec->next; } while(tmpl1!=NULL){ int res = carry + tmpl1->val; carry = res / 10; res %= 10; tmpl1 = tmpl1->next; tmprec->next = new ListNode(res); tmprec = tmprec->next; } while(tmpl2 != NULL){ int res = carry + tmpl2->val; carry = res / 10; res %= 10; tmpl2 = tmpl2->next; tmprec->next = new ListNode(res); tmprec = tmprec->next; } while(carry > 0){ ListNode* tmpptr = new ListNode(carry % 10); //carry /= 10; tmprec->next = new ListNode(carry % 10); carry /= 10; tmprec = tmprec->next; } return dummy.next; }};
错误1:
每次处理余下的位和进位的问题
错误2:
第一次的tmprec初始化为&dummy
0 0
- LeetCode: Add Two Numbers
- LeetCode: Add Two Numbers
- [LeetCode]Add Two Numbers
- LeetCode Add Two Numbers
- Leetcode: Add Two Numbers
- LeetCode : Add Two Numbers
- LeetCode Add Two Numbers
- [LeetCode] Add Two Numbers
- Leetcode : Add Two Numbers
- 【leetcode】Add Two Numbers
- [LeetCode]Add Two Numbers
- leetcode Add Two Numbers
- LeetCode-Add Two Numbers
- [leetcode] Add Two Numbers
- [LeetCode] Add Two Numbers
- LeetCode - Add Two Numbers
- LeetCode | Add Two Numbers
- 【leetcode】Add Two Numbers
- Hadoop 介绍
- 第一章 统计学习方法概论
- 算法与数据结构——更新
- 黑马程序员——Foundation之集合类
- 知难而进 ——反思我带的第一个版本
- leetcode Add Two Numbers
- 设计模式六大原则(4):接口隔离原则
- 挑战2.3(POJ 3616 Milking Time)
- 实例4:osganimationsolid例子实现关键帧插值动画(OSG程序开发实例集)
- HDOJ Networking
- Android入门实例三 注册界面的设计与实现
- nodeJs(多行数据删除)
- stm32 精确个数高速脉冲输出
- 网络电台mms地址大全(一)