题目等级: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. Azure Event hub usage

    1. create event hub on azure watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/40 ...

  2. POJ2481:Cows(树状数组)

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  3. 5分钟Serverless实践 | 构建无服务器图片鉴黄Web应用

    Serverless是什么 Serverless中文译为“无服务器”,最早可以追溯到2012年Ken Fromm发表的<Why The Future Of Software And Apps I ...

  4. 【Ubuntu】某灯图标过大

    发现某灯在桌面上图标过大,点击resize拖动没反应,遂查找原因 发现是它图标只有128x128的版本,所以显得比别的图标大 打开lantern.desktop(不知道.desktop请看https: ...

  5. (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作

    上午写了下单向循环链表的程序,今天下午我把双向链表的程序写完了.其实双向链表和单向链表也是有很多相似的地方的,听名字可以猜到,每个节点都包含两个指针,一个指针指向上一个节点,一个指针指向下一个节点.这 ...

  6. BZOJ 3343: 教主的魔法(分块+二分查找)

    BZOJ 3343: 教主的魔法(分块+二分查找) 3343: 教主的魔法 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1172  Solved:  ...

  7. UVALive 4671 K-neighbor substrings 巧用FFT

    UVALive4671   K-neighbor substrings   给定一个两个字符串A和B B为模式串.问A中有多少不同子串与B的距离小于k 所谓距离就是不同位的个数. 由于字符串只包含a和 ...

  8. JSP-Runoob:JSP Session

    ylbtech-JSP-Runoob:JSP Session 1.返回顶部 1. JSP Session HTTP是无状态协议,这意味着每次客户端检索网页时,都要单独打开一个服务器连接,因此服务器不会 ...

  9. la3713

    2-sat...求解2-sat方案直接每个变量枚举就行了,lrj的代码很靠谱... #include<bits/stdc++.h> using namespace std; ; struc ...

  10. IDEA Spark程序报错处理

    错误一: // :: ERROR Executor: Exception ) java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Prod ...