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. C语言精神

    国际标准化组织与1990年发布了第一个ANSI/ISO C标准 在该委员会制定的指导原则中,最有趣的可能是:保持C的精神.委员会在表达这一精神时列出了一下几点: 信任程序员: 不要妨碍程序员做需要做的 ...

  2. Uva 10892 LCM Cardinality (数论/暴力)

    题意:给出数n,求有多少组A,B的最小公约数为n; 思路:3000ms,直接暴力寻找,找到所有能把n整除的数 pi, 枚举所有pi 代码: #include <iostream> #inc ...

  3. 裴波那序列-JAVA实现

    编程输出:裴波那序列,1000项,int会越界! BigInteger  [] pArr=new BigInteger [10000];           pArr[0]=new BigIntege ...

  4. MyBetis3.2框架技术

    1.1    MyBatis介绍 MyBatis 世界上流行最广泛的基于SQ语句的ORM框架,由Clinton Begin 在2002 年创建,其后,捐献给了Apache基金会,成立了iBatis 项 ...

  5. Struts2基础学习(四)—类型转换器和数据校验

    一.自定义类型转换器 1.概述      Struts2提供了常规类型转换器,可以用于常用数据类型的转换,但如果目标类型是一个特殊类型,则需要自定义转换器.Struts2 类型转换器实际上都是基于OG ...

  6. Android使用本地广播

    Android本地广播学习中一直被忽略,今天用到了,Mark一下 1.本地广播的定义和普通广播一样 例如 public class WakeTimesBroadcastReceiver extends ...

  7. Kubernetes DNS 简介

    环境 $ sudo lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16 ...

  8. 使用Microsoft SQL Server Migration Assistant for Oracle迁移数据库

    前言:使用Microsoft SQL Server Migration Assistant for Oracle迁移Oracle数据库到SqlServer数据库. 准备:Oracle11g.SqlSe ...

  9. 实现一个竖直的显示表头的表格(vue版本)

    今天遇到一个问题,实现这样一个竖直的显示表头的表格,如下图.默认显示两列. vue实现代码如下:   tableComponent.vue:   <template> <table ...

  10. Java解决TopK问题(使用集合和直接实现)

    在处理大量数据的时候,有时候往往需要找出Top前几的数据,这时候如果直接对数据进行排序,在处理海量数据的时候往往就是不可行的了,而且在排序最好的时间复杂度为nlogn,当n远大于需要获取到的数据的时候 ...