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题解的更多相关文章

  1. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  2. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  3. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  5. Leetcode:Add Two Numbers分析和实现

    Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...

  6. [Leetcode] Add two numbers 两数之和

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. [LeetCode] Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. LeetCode——Add Two Numbers

    Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...

  9. [LeetCode] Add Two Numbers 链表

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

随机推荐

  1. Codeforces Global Round 2部分题解

    传送门 好难受啊掉\(rating\)了-- \(A\ Ilya\ and\ a\ Colorful\ Walk\) 找到最后一个与第一个颜色不同的,比一下距离,然后再找到最左边和最右边与第一个颜色不 ...

  2. leetcode-840-Magic Squares In Grid

    题目描述: A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each r ...

  3. 华南理工大学“三七互娱杯”程序设计竞赛 HRY and codefire(概率期望DP)

    https://ac.nowcoder.com/acm/contest/874/A 题目:有两个账号 , 一开始都为0级 , 求任意一个账号升级到N的期望 要求:如果当前账号嬴了 , 就继续沿用当前的 ...

  4. Powershell cannot be loaded because running scripts is disabled on this system 解决办法

    问题背景 第一次跑ps时,出现了下面的提示.这是因为windows不允许执行脚本而已,不要大惊小怪. 解决办法 这个需要管理员执行,不然会出现以下的情况 正常情况

  5. 【第2次会议记录_2018.5.27】—— [ 算法原理 ]:手工特征提取的概念问题。(by_wanghao)

    1.提取 特征点 .特征描述子 与 提取特征向量 之间的区别: (1).特征点:指的是一张图片上比较有代表性的‘位置’,提取特征点就是把图片中这些有代表性的位置给标出来. (2).特征描述子:当提取出 ...

  6. C#中子类和父类

    在实例化子类的时候,总是先调用父类的无参构造函数

  7. (转)ELK Stack 中文指南--性能优化

    https://www.bookstack.cn/read/ELKstack-guide-cn/elasticsearch-README.md https://blog.csdn.net/cjfeii ...

  8. web前端优化之reflow(减少页面的回流)

    1.什么是reflow? reflow(回流)是指浏览器为了重新渲染部分或者全部的文档,重新计算文档中的元素的位置和几何构造的过程. 因为回流可能导致整个Dom树的重新构造,所以是性能的一大杀手. 以 ...

  9. 【文档】五、Mysql Binlog事件结构

    这个部分描述了事件被写入binlog或者delay log中的属性.所有的事件有相同的整体结构,也就是包含事件头和事件数据: +===================+ | event header ...

  10. Java 8学习之Lambda表达式

    一.lambda表达式 一个lambda表达式包含三个部分: 一段代码 参数 自由变量的值,这里的自由指的是哪些不是参数并且没有在代码中定义的变量. 示例: public static void re ...