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

  题目如上述所示。

  大概翻译:

  给出两个非空的链表代表两个非负的整数。数字以相反的顺序存储,每个节点包含一个数字,将两个数相加并把结果作为一个链表返回。

  你可以假设两个数除了本身是0以外都没有前导0。

  本题最关键的地方在于解决进位问题。

  在网上查询了一些解法,包括借鉴了这篇:http://blog.csdn.net/ljiabin/article/details/40476399

  其中对于进位问题的解决在一开始便引入一个节点以便最后有进位时使用个人觉得有些不妥,并且使代码不太容易懂。

  下面给出我对于此问题的解法:

  【Java代码】

  

/**
* 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) {
//如果给出就为空,则直接返回另外一个链表
if(l1 == null) return l2;
if(l2 == null) return l1; int flag = 0;//存放进位信息,但是并不是处理最后的进位标志 //构造返回结果的第一个节点
ListNode result = new ListNode((l1.val + l2.val) % 10);
ListNode p = result;
flag = (l1.val + l2.val) / 10; l1 = l1.next;
l2 = l2.next; while(l1!=null || l2!=null){
int l1Num = (l1==null)?0:l1.val;//如果l1链表为空,则视值为0
int l2Num = (l2==null)?0:l2.val;//如果l2链表为空,则视值为0
p.next = new ListNode((l1Num + l2Num + flag) % 10);
p = p.next;
flag = (l1Num + l2Num + flag) / 10;
if(l1 != null){
l1 = l1.next;
}
if(l2 != null){
l2 = l2.next;
} }
    //处理最后的进位问题
if(flag != 0){
p.next = new ListNode(flag);
p = p.next;
} return result;
}
}

  

  

如果有任何问题,欢迎跟我联系:xiaomenxiaomen@qq.com

我的github地址:github.com/WXRain

LeetCode---------Add Two Numbers 解法的更多相关文章

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

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

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

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

  3. LeetCode Add Two Numbers II

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

  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题解

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

  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

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

  9. LeetCode——Add Two Numbers

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

  10. [LeetCode] Add Two Numbers 链表

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

随机推荐

  1. 3.Java集合总结系列:Set接口及其实现

    一.Set接口 Set 接口与 List 接口相比没有那么多操作方法,比如: 1.List 接口能直接设置或获取某个元素的值,而Set接口不能. 2.List 接口能直接在指定位置删除.增加元素,而S ...

  2. 1.Redis 的安装

    一.Redis 介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 作为Key-value型数据库,Red ...

  3. win10 平台 elasticsearch 与 elasticsearch-head 的安装

    由于elasticsearch是基于java开发的,所以 第一步需要安装JDK. 具体JDK的安装步骤  http://jingyan.baidu.com/article/6dad5075d1dc40 ...

  4. 写出稳定的Modbus代码之点滴经验

    1.引言 Modbus是工业领域重要的协议,物理层有常见的RS485双绞线和TCP,所以又常说Modbus 485开发和Modbus TCP开发. 前者就是串口通信,比较简单.后者涉及到网络协议,复杂 ...

  5. JS实现轻量级计算器

    想尝试做一个网页版计算器后,参考了很多博客大神的代码,综合归纳.总结修改,整理如下文. 附:   Demo    源码 一.HTML+CSS 具体结构样式如下图,基本参照手机计算器界面.按钮功能可以查 ...

  6. 文件系统与linux相关知识点

    文件系统是操作系统中管理持久性数据的子系统,提供数据存储和访问功能.对于服务器开发人员,比较关注的是unix(linux)环境下的文件系统,比如分区与磁盘关系,磁盘的剩余空间,文件的类型与权限控制,文 ...

  7. js继承之原型链方式实现

    温故而知新: 在之前的文章已经重点了解了原型对象,今天就理一理这个原型对象在原型链式的继承中的使用 function a(x,y){this.x=x;this.y=y;} //定义一个函数,当构造函数 ...

  8. struts2 之 Action的创建方式

    总结:struts2是一个轻量级框架,提供了无侵入性的实现方式,struts2也提供了接口和类来实现action.通过实现接口或者继承类来实现action可以实现struts2提供的相关功能, 1. ...

  9. mybatis新手入门常见问题集(持续更新)

    一.参数为集合 Q:parameterType指的的类型是集合类型还是对象? A:都可以,甚至不用在xml中指定也可以.第一,mybatis会对传入的参数进行判断是不是list或者array,第二,m ...

  10. React之key详解

    一个例子 有这样的一个场景如下图所示,有一组动态数量的input,可以增加和删除和重新排序,数组元素生成的组件用index作为key的值,例如下图生成的ui展示: 上面例子中的input组件渲染的代码 ...