我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/

`

2.Add Two Numbers [M]

题目

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

思路

题目的大意是两个链表求和,和也是一个链表(链表是逆序存的数字)
这和大数的加减很像,不过这个进位是从后向前。
所以可以同时从前往后加,然后保留进位。

代码

我的这个代码写的不够好,考虑的过于复杂,可以直接看下面更精巧的代码。

/**
* 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) {
//异常判断
if(l1 == NULL && l2 == NULL)
return NULL;
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
//初始化
ListNode * result = new ListNode((l1->val + l2->val)%10);
int carry = (l1->val + l2->val)/10; //表示进位
ListNode* templ1 = l1;
ListNode* templ2 = l2;
ListNode* tempresult = result;
//这里弄一个结束节点,有利于当一个节点到结尾后做操作
ListNode* EndNode = new ListNode(0);
while(templ1->next != NULL || templ2->next != NULL)
{
//如果某个链表已经到结尾了,那么就把它变成EndNode,特点是值为0,next = NULL
if(templ1->next == NULL)
templ1 = EndNode;
else
templ1 = templ1->next;
if(templ2->next == NULL)
templ2 = EndNode;
else
templ2 = templ2->next;
tempresult->next = new ListNode((templ1->val + templ2->val + carry)%10);
carry = (templ1->val + templ2->val + carry)/10; tempresult = tempresult->next; }
if(carry == 1)
tempresult->next = new ListNode(1);
return result; }
};

更精巧的代码

不得不说,potpie的代码比我上面的代码精简了不少,我上面的代码考虑的太多了,因为我通式用的templ1->val + templ2->val + carry,导致每次需要对先结束的链表进行尾部填充,而且开头多了很多额外的代码。
他的代码,2个链表是独立操作的,而且代码写的很精简,基本没废话!赞

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode c1 = l1;
ListNode c2 = l2;
ListNode sentinel = new ListNode(0);
ListNode d = sentinel;
int sum = 0;
while (c1 != null || c2 != null) {
sum /= 10;
if (c1 != null) {
sum += c1.val;
c1 = c1.next;
}
if (c2 != null) {
sum += c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum % 10);
d = d.next;
}
if (sum / 10 == 1)
d.next = new ListNode(1);
return sentinel.next;
}
}

《LeetBook》LeetCode题解(2):Add Two Numbers [M]的更多相关文章

  1. leetcode题解2. Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  2. LeetCode题解 #2 Add Two Numbers

    题目大意:使用链表表示的两个整数,计算出其和,以同样的形式返回. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...

  3. LeetCode 题解之Add Two Numbers II

    1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { Lis ...

  4. LeetCode题解之Add two numbers

    1.题目描述 2.题目描述 题目思路可以参考合并单链表的思路,定义一个全局 进位标志,如果两个数值相加得到需要进位,则将进位标志置为1 . 3.代码 ListNode* addTwoNumbers(L ...

  5. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  6. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  7. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  8. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  9. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

随机推荐

  1. Array对象的创建及其操作方法

    一.创建数组,即实例化数组对象      有三种方式:1. new Array();                          2.new Array(size);               ...

  2. ZSTU4269 买iphone 2017-03-22 14:31 73人阅读 评论(0) 收藏

    4269: 买iphone Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 1710  Solved: 316 Description 自从上次仓鼠中了 ...

  3. [翻译] Writing Property Editors 编写属性编辑器

    Writing Property Editors 编写属性编辑器   When you select a component in the designer its properties are di ...

  4. eayui js动态加载Datagrid,自适应宽度,高度

    HTML: <div class="easyui-layout" style="min-height:100%;min-width:100%;"> ...

  5. MVC页面移除HTTP Header中服务器信息

    默认情况下,每一个MVC请求的HTTP Header中都会包含着当前服务器的一些信息,出于安全还是性能还是处女座的强迫症等等,都想把这些信息移除掉,增加一些应用程序的神秘感,如下,默认情况下Chrom ...

  6. 微信小程序开发中的二三事之网易云信IMSDK DEMO

    本文由作者邹永胜授权网易云社区发布. 简介 为了更好的展示我们即时通讯SDK强悍的能力,网易云信IM SDK微信小程序DEMO的开发就提上了日程.用产品的话说就是: 云信 IM 小程序 SDK 的能力 ...

  7. Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)

    问题: Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS) 解决: cmake -DPYTHON_INC ...

  8. BZOJ3786: 星系探索(伪ETT)

    题面 传送门 题解 坑啊--我好像把\(Splay\)的东西全忘光了-- \(ETT\)(\(Euler\ Tour\ Tree\))是一种可以资瓷比\(LCT\)更多功能的数据结构,然而不管是功能还 ...

  9. Java Web 学习与总结(三)会话跟踪

    何为会话跟踪?举个简单的例子,比如登陆到某购物网站后,在一定时间内无论你在这个网站中切换到任意的网页,只要不执行退出操作,一直保持着你账号的登录状态. 那么在Java Web中我们应当如何去实现这一操 ...

  10. php请求远程url内容方法

    php请求远程url内容有两个方法fopen/file_get_contents和curl. 1,fopen/file_get_contents与curl的差异 (1)fopen /file_get_ ...