一道比较基本的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. JSP或HTML命名规范

    1.jsp与html文件名全部小写 2.数据/内容显示页 名词形式,多个单词用下划线分隔,要求能说明显示内容的信息,为避免冲突,可加上“_list”或者其他的单词.例如:news_message.ht ...

  2. JavaScript —— attachEvent 方法的使用

    动态地给一个对象添加事件(方法). 直接上代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ...

  3. eclipse中tomcat使用add and remove无法发布web项目

    继上次启动eclipse中的tomcat报classNotFound的问题后,这次又遇到新问题.就是右键点击tomcat使用add and remove发布web项目至tomcat后,启动tomcat ...

  4. Create Entity Data Model

    http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx 官 ...

  5. [CF660C]Hard Process(尺取法)

    题目链接:http://codeforces.com/problemset/problem/660/C 尺取法,每次遇到0的时候补一个1,直到补完或者越界为止.之后每次从左向右回收一个0点.记录路径用 ...

  6. Linux Kernel CMPXCHG函数分析

    原文地址:http://blog.csdn.net/penngrove/article/details/44175387 最近看到Linux Kernel cmpxchg的代码,对实现很不理解.上网查 ...

  7. java生成带html样式的word文件

    参考:http://blog.csdn.net/xiexl/article/details/6652230 最近在项目中需要将通过富文本编辑器处理过的文字转换为Word,查了很久,大家通常的解决办法是 ...

  8. 运行时报错 ADB server didn’t ACK

    查看进程中所有和ADB有关的进程,全都结束了,包括什么豌豆荚之类的(大多数情况是占用端口),之后重新启动Eclipse.

  9. 导出excel小结(C#,.NET,Wpf)

    range.NumberFormatLocal = "@";     //设置单元格格式为文本      range.NumberFormatLocal = "@&quo ...

  10. Spring 实践 -IoC

    Spring 实践 标签: Java与设计模式 Spring简介 Spring是分层的JavaSE/EE Full-Stack轻量级开源框架.以IoC(Inverse of Control 控制反转) ...