[LeetCode] 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 II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode——Add Two Numbers
Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- java的string和==和equals和hashcode简单理解
String s1= "abc"; s1是引用变量,在栈里面,如果java的String常量池中没有abc,则开拓一块区域存abc,s1指向常量池中的abc: String s2= ...
- python学习笔记-练手实例
1.题目:输出 9*9 乘法口诀表. 程序分析:分行与列考虑,共9行9列,i控制行,j控制列 代码: for i in range(1,10): print ('\r') for j ...
- LG的nexus5(32GB版本 - 821)-TOT-底包 可用于救砖!
LG的nexus5(32GB版本 - 821)-TOT-底包 底层修复效果完美,通过LGflashTool1.8直接刷进去就行~ 底包下载: https://pan.baidu.com/s/1Z5WD ...
- java爬虫中jsoup的使用
jsoup可以用来解析HTML的内容,其功能非常强大,它可以向javascript那样直接从网页中提取有用的信息 例如1: 从html字符串中解析数据 //直接从字符串中获取 public stati ...
- Spring注入方式(1)
Spring支持3种依赖注入方式,分别为属性注入.构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解. 1.常量注入 属性注入是通过setter方法注入Bean的属性 ...
- Sequential Minimal Optimization(SMO,序列最小优化算法)初探
什么是SVM SVM是Support Vector Machine(支持向量机)的英文缩写,是上世纪九十年代兴起的一种机器学习算法,在目前神经网络大行其道的情况下依然保持着生命力.有人说现在是神经网络 ...
- (转)Linux下同步工具inotify+rsync使用详解
原文:https://segmentfault.com/a/1190000002427568 1. rsync 1.1 什么是rsync rsync是一个远程数据同步工具,可通过LAN/WAN快速同步 ...
- Linux网络编程服务器模型选择之并发服务器(上)
与循环服务器的串行处理不同,并发服务器对服务请求并发处理.循环服务器只能够一个一个的处理客户端的请求,显然效率很低.并发服务器通过建立多个子进程来实现对请求的并发处理.并发服务器的一个难点是如何确定子 ...
- happy in java之io流简介
闲来没事,重温马士兵老师的java基础... 流 水流的流 流氓的流,,流 英文叫做stream,溪流 流的分类: 流是用来读写数据的. 流就像水流一样,File类封装的是文件的名字,它是内存里头 ...
- PHP之string之ltrim()函数使用
ltrim (PHP 4, PHP 5, PHP 7) ltrim - Strip whitespace (or other characters) from the beginning of a s ...