[LeetCode] Add Two Numbers题解
来源:互联网 发布:我怎么投诉淘宝店铺 编辑:程序博客网 时间:2024/06/05 12:40
Add Two Numbers:
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
这是LeetCode中标记为Medium的一道题。其实这道题的思路非常简单,两个链表中每个对应节点的值相加作为第三个链表对应节点的值,考虑进位即可。但是因为太久没有做链表的题,做题的时候遇到了一些陷阱,并且最后Accepted的代码有四十行。看到Discussion中有人只用了十几行就解决了问题,非常钦佩(同时非常赧然)。
以下是两份代码的对比:
public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode re(0), *p = &re; int carry = 0; while(l1 || l2 || carry){ int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) +carry; carry = sum / 10; p->next = new ListNode(sum % 10); p = p->next; l1 ? l1 = l1->next : l1; l2 ? l2 = l2->next : l2; } return re.next; }};
public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int a = 0,b = 0,carry = 0;//ab是用来记录两个链表的长度的,carry是进位 int ta = 0,tb = 0,tc = 0;//ta、tb是两个链表里头的一个数,tc是他们之和 ListNode* temp1 = l1; ListNode* temp2 = l2; ListNode* p = new ListNode(0); ListNode* re= p; while(temp1 != NULL){ a ++; temp1 = temp1->next; } while(temp2 != NULL){ b ++; temp2 = temp2->next; } int max = a > b? a : b; for(int i = 0; i < max; i++){ ta = tc = tb = 0; if(l1 != NULL){ ta = l1->val; l1 = l1->next; } if(l2 != NULL){ tb = l2->val; l2 = l2->next; } tc = (carry + ta + tb) % 10; carry = (carry + ta + tb) / 10;//进位 p->next = new ListNode(tc); p = p->next; } //可能最后有进位,所以再执行一次 if(ta+tb+carry>9){ p->next = new ListNode(1); p = p->next; } return re->next; }};
做题中犯的错误
最初的指针用的是这种写法
ListNode *p;ListNode *re= p;...p = new ListNode(tc);p = p->next;...return re;
最后发现每次返回的re都是NULL。其实是因为自己犯了很傻的错误,虽然初始化时将指针p赋值给指针re,看起来好像re和p相等了,但是指针p执行了一次new ListNode之后指向了别的地址,所以指针re和p是不等的,这时候返回re当然就是空值了。
而后来的写法,由于p和re初始化指向一个node实例,并且执行new操作的是p->next,所以不会出现这种问题。
引以为鉴啊!
- LeetCode题解:Add Two Numbers
- LeetCode题解:Add Two Numbers
- [LeetCode] Add Two Numbers题解
- LeetCode 题解(26): Add Two Numbers
- LeetCode题解 || Add Two Numbers 问题
- [Leetcode题解]002 Add Two Numbers
- LeetCode题解--2. Add Two Numbers
- 2. Add Two Numbers : LeetCode 题解
- LeetCode题解:2. Add Two Numbers
- leetcode 题解 2. Add Two Numbers
- Leetcode题解 - 2. Add Two Numbers
- LeetCode题解-2-Add Two Numbers
- LeetCode Algorithms 2. Add Two Numbers 题解
- LeetCode 2. Add Two Numbers 题解
- 2. Add Two Numbers LeetCode题解
- LeetCode题解(2)--Add Two Numbers
- LeetCode题解(Golang实现)--Add Two Numbers
- LeetCode题解 #2 Add Two Numbers
- [知了堂学习笔记]_牵线Eclipse和Tomcat第二篇 —— 安装Tomcat&&添加Tomcat到Eclipse
- Spatial Transformer Networks
- 梯度提升树GBDT原理
- linux-centos安装solr及通过zookeeper搭建solr集群
- Python学习入门
- [LeetCode] Add Two Numbers题解
- JAVA语言:求1+2!+3!+...+20!的和
- 合并两个排序的链表
- [知了堂学习笔记]_记一次BootStrap的使用
- springboot 获取普通Bean
- 湖南省第十三届大学生计算机程序设计竞赛 Football Training Camp 贪心
- Matlab\Simulink仿真中遇到的代数环问题
- [知了堂学习笔记]_eclipse引入svn插件,并将项目同步到svn
- 校招准备系列:每天一道算法题(6)-连续最大和