一道比较基本的LinkedList的题目。题目要求是这样,现在有两个LinkedList,(2  -> 4 -> 3)和(5 -> 6 -> 4),然后从头开始,把每个node的value相加,最终输出的结果应该是:(7 ->0 ->8)。这里需要注意的边界条件有两个,一个是两个链表长度不一样的问题,另一个是进位的问题,注意了这两个问题以后,问题就比较好解决了。

  具体的代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//对于链表来说,当需要处理头节点head的时候,本人比较喜欢用一个dump node, 通过
//dump node来指向头节点,从而省去很多判断是否为head的情况。最后返回dumpNode.next即可
ListNode dumpNode = new ListNode(0);
//用add来纪录是否进位。
int add = 0;
ListNode curr = dumpNode;
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + add;
//记录是否该进位,当sum > 9,则需要进位。
if (sum > 9) {
sum = sum % 10;
add = 1;
}
else add = 0;
curr.next = new ListNode(sum);
curr = curr.next;
l1 = l1.next;
l2 = l2.next;
} //l1长度>l2,则进入该循环,注意进位。
while (l1 != null) {
int sum = l1.val + add;
if (sum > 9) {
sum = 0;
add = 1;
}
else add = 0;
curr.next = new ListNode (sum);
curr = curr.next;
l1 = l1.next;
}
//l2 > l1的情况
while (l2 != null) {
int sum = l2.val + add;
if (sum > 9) {
sum = 0;
add = 1;
}
else add = 0;
curr.next = new ListNode(sum);
curr = curr.next;
l2 = l2.next;
}
//这个容易漏掉,来说明最后是否进位,如 [5]和[5]最终应输出[0->1]
if (add == 1) curr.next = new ListNode(1); return dumpNode.next;
}
}

LeetCode(2) - Add Two Numbers的更多相关文章

  1. leetcode 第二题Add Two Numbers java

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

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

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

  3. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

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

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

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

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

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

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

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

  7. 【一天一道leetcode】 #2 Add Two Numbers

    一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...

  8. leetcode题解2. Add Two Numbers

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

  9. 【LeetCode练习题】Add Two Numbers

    链表相加 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

随机推荐

  1. poj -2229 Sumsets (dp)

    http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...

  2. [HDOJ4325]Flowers(树状数组 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...

  3. 【转载】正则表达式学习 & ASCII码表

    文章原地址: http://www.jb51.net/tools/zhengze.html <正则表达式30分钟入门教程> 其中有几个地方可以有笔记: \s 匹配任意的空白符 \b 匹配单 ...

  4. laravel named route

    laravel中一般对于路由的使用方法是在routes.php中定义一个路由,在view中如果要引用一个url则直接通过<a href="url/">来使用. 但是随着 ...

  5. BZOJ 4408 神秘数

    题解同各神犇的方法... #include<iostream> #include<cstdio> #include<cstring> #include<alg ...

  6. android系统掉电保护

    /************************************************************************ * android系统掉电保护 * 说明: * An ...

  7. UITabbar item 设置笔记

    很长一段时间都是用代码来写UITabbarController,试着用xib来写一次,但是遇到tabbar item的图标自定义的时候不知道从何入手,比如定义选定前和选定后的icon图片,这地方还是不 ...

  8. trunc的使用

    1.日期比较时精确到日,可以使用 TRUNC(sysdate,'dd')函数.函数支持格式有:yyyy MM  dd  hh Mi可以用 select TRUNC(sysdate,'yyyy') fr ...

  9. 【转】NSArray排序方法

    原文网址:http://www.cnblogs.com/xiaobaizhu/archive/2013/06/05/3119983.html 从网上查的,非常方便的排序api,功能也很强大 1.sor ...

  10. Android 图文教学让你彻底理解activity启动模式

    我们首先从最简单的开始, standard 这个模式就是默认的模式,我们都知道 当你用这个模式时,每次发送一个intent,都会生成一个新的实例! 我写一个简单的例子: <?xml versio ...