第一天 Add Two Numbers(链表加法)
来源:互联网 发布:php java 比较 编辑:程序博客网 时间:2024/06/16 00:16
这是leetcode第二题,对链表的知识基础有一定要求。暂时自己写不出来,先完全解析别人的代码。
1. 先排除极端情况,简化后续
<span style="font-size:14px;"> if(l1 == NULL && l2) return l2; if(l1 && l2 == NULL) return l1; if(l1 == NULL && l2 == NULL) return NULL;</span>
2. p1,p2为了避免对原输入的影响;head用于最后的输出;c是进位问题;r是随着链表深入下去;bUseList2是决定r沿着l1或者l2进行
由于l1,l2有完整的链表体系,所以沿着l1,l2的next可以避免新生成对象的麻烦。换而言之,借用现有的“变量”l1或者l2,修改成为result
<span style="font-size:14px;"> ListNode * p1 = l1; ListNode * p2 = l2; ListNode *r = l1; // r indicates the node space we will use for one digit of the result. ListNode *head = r; // head is the result list. bool bUseList2 = false; // Use this to indicate whether we should start to use l2 as resource pool. int c = 0;</span>
3. while语句表达了三种情况,即l1,l2耗尽以及没有进位的情况(考虑:怎么处理l1,l2同时耗尽且进位的情况);r = r->next正是借用l1,l2的巧妙之处。
<span style="font-size:14px;">while(p1 || p2 || c){ int v = c; if(p1) v += p1->val; if(p2) v += p2->val; c = v >= 10? 1: 0; r->val = v % 10; if(p1) p1 = p1->next; if(p2) p2 = p2->next; // If we haven't started to used l2, and we have reached the tail of l1, // switch l2 for the next result digit. if(bUseList2 == false && r->next == NULL){ r->next = l2; bUseList2 = true; } if(p1 || p2 || c) r = r->next; }</span>
最后把r->next用NULL堵上即可。
完整代码如下:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { // first take care of the empty list cases. if(l1 == NULL && l2) return l2; if(l1 && l2 == NULL) return l1; if(l1 == NULL && l2 == NULL) return NULL; ListNode * p1 = l1; ListNode * p2 = l2; ListNode *r = l1; // r indicates the node space we will use for one digit of the result. ListNode *head = r; // head is the result list. bool bUseList2 = false; // Use this to indicate whether we should start to use l2 as resource pool. int c = 0; while(p1 || p2 || c){ int v = c; if(p1) v += p1->val; if(p2) v += p2->val; c = v >= 10? 1: 0; r->val = v % 10; if(p1) p1 = p1->next; if(p2) p2 = p2->next; // If we haven't started to used l2, and we have reached the tail of l1, // switch l2 for the next result digit. if(bUseList2 == false && r->next == NULL){ r->next = l2; bUseList2 = true; } if(p1 || p2 || c) r = r->next; } // Set the tail of the result list to NULL. r->next = NULL; return head; }
0 0
- 第一天 Add Two Numbers(链表加法)
- LeetCode2. Add Two Numbers(链表加法)
- 2. Add Two Numbers(链表加法)
- #2 Add Two Numbers (链表作加法)
- [C++]LeetCode 2: Add Two Numbers(链表逆序加法)
- [leetcode] add two numbers(大数据加法链表版)
- LeetCode-445. Add Two Numbers II (JAVA)链表数字加法
- LeetCode Add Two Numbers(用链表模拟加法)
- 【一天一道leetcode】 #2 Add Two Numbers
- LeetCode | Add Two Numbers(两个链表相加)
- [LeetCode-2] Add Two Numbers(链表数据之和)
- 167.Add Two Numbers-链表求和(容易题)
- LeetCode 2 Add Two Numbers(链表操作)
- Leetcode 2. Add Two Numbers(链表求和)
- Add Two Numbers 链表相加
- Add Two Numbers 链表相加
- Add Two Numbers Leetcode 链表相加
- Add Two Numbers 链表基本应用
- php使用Mailer实现发送邮件
- android——ToggleButton按钮
- python多线程(2)--thread和threading模块的使用
- 网站开发进阶(四十二)巧用clear:both
- Add Digits
- 第一天 Add Two Numbers(链表加法)
- 北漂-代码大神成长的第一天
- XZ_iOS之打开QQ临时会话列表和拨打电话
- 【codevs1298】凸包周长,计算几何
- UIStatusBar 屏幕顶部用于显示网络、时间和电量等的、高度为20点的控件
- ZIP文件格式组成
- 带你走进Android微信支付和支付宝支付服务端操作放到客户端的故事
- Exit()
- 开发环境搭建3:linux下tuxedo安装