题目等级:Medium

题目描述:

  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.

  Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

  题意:给定两个链表,每个链表代表一个整数,但是是逆序存储的,求两个链表代表的数的和,结果仍然逆序保存在链表中。


解题思路:

  本题实际上也比较简单,两个链表逆序存储,相当于从最低位开始,根据加法运算,只需要把低位相加,然后将进位传递为高位,高位加的时候多考虑一个进位就可以了,很好理解。

  具体过程直接看下面的代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1==null)
return l2;
if(l2==null)
return l1;
ListNode head=new ListNode(-1);
ListNode cur=head;
int carry=0; //进位
while(l1!=null || l2!=null){
int num1=l1==null?0:l1.val;
int num2=l2==null?0:l2.val;
int sum=num1+num2+carry; //低位依次相加
carry=sum/10; ListNode temp=new ListNode(sum%10);
temp.next=null;
cur.next=temp;
cur=temp;
if(l1!=null) l1=l1.next;
if(l2!=null) l2=l2.next;
}
if(carry!=0){ //最后是否还有一位进位,要注意判断,比如:5+5
cur.next=new ListNode(carry);
cur.next.next=null;
}
return head.next;
}
}

  扩展:

  What if the the digits in the linked list are stored in non-reversed order? For example:

  (3→4→2)+(4→6→5)=8→0→7

  本题最后给出了一个扩展,当链表的数不是逆序存储,而是从高位到低位正序存储时,该怎么办?

  很明显,最直观的想法就是不是逆序就先将链表进行反转,变为逆序,然后再利用本题的思路解决。那么这里的关键问题就是如何反转链表?

  实际上,反转链表这道题目在多个地方出现,可以使用三指针或者递归的解法,具体可以参考另外一篇博客:【剑指Offer】15、反转链表

【LeetCode】2、Add Two Numbers的更多相关文章

  1. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  2. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  3. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  4. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  5. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  6. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  7. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  8. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  9. 【LEETCODE】64、链表分类,medium&hard级别,题目:2,138,142,23

    package y2019.Algorithm.LinkedList.medium; import y2019.Algorithm.LinkedList.ListNode; /** * @Projec ...

随机推荐

  1. CSDN学院 免费技术答疑公开课,本周六场即将开播~~~

    为了酬谢广大学员.CSDN学院特推出免费技术答疑公开课.让您开启一段充实的学习之旅~ 本周六场即将开播. ----------------------------------------------- ...

  2. centos命令行安装mysql随机密码查看方法(遇到问题及其解决办法)

    mysql初次命令行安装登录时报错: 未输入密码:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwo ...

  3. shell学习三十七天----引用

    引用 案例,假设我想输出一个星号(*),使用echo怎样做? echo * 这是肯定不行的,须要将*转移,即:echo \* 这样就引出了引用的概念.所为引用,是用来防止shell将某些你想要的东西解 ...

  4. 【C语言】不使用大小于号,求出两数最大值

    //不使用大小于号,求出两数最大值 #include <stdio.h> #include <math.h> double Max(double a, double b) { ...

  5. JavaScript检查是否包含某个字符

    转自:http://my.oschina.net/u/1450300/blog/389325 indexOf用法: indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置.如 ...

  6. hadoop hbase 快速启动 关闭 配置:

    vim ~/.bashrcexport JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64   //JDK安装路径export HADOOP_HOME=/usr/l ...

  7. [Swift通天遁地]七、数据与安全-(7)创建文件浏览器:以可视化的方式浏览沙箱文件

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. Akka源码分析-故障恢复

    Actor故障恢复是akka中非常重要的内容,在之前的博客中虽然有介绍,但都是杂糅在其他知识点的细节中,本博客将单独介绍这一部分的故障恢复.为了简化分析的单独,本文只研究用户的actor故障恢复的步骤 ...

  9. php从数据库读取中文显示问号??的解决办法

    出错原因:1.数据库编码格式不对 2.PHP编码格式不对 3.浏览器编码格式不对 上面三者编码格式不统一,就会出现问题 数据库读取的时候在mysqli_connect()之后要设置连接字符编码mysq ...

  10. WP8开发常用解决方案收集

    我其实不怎么做wp的东西.但是偶尔还是会用到, 但是wp8开发的资料确实难找.特开此贴,记录一些常见的解决方案 1.水平滑动动画(比如app首次使用说明就可以用这个做) http://www.cnbl ...