【LeetCode】2、Add Two Numbers
题目等级: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的更多相关文章
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【LEETCODE】64、链表分类,medium&hard级别,题目:2,138,142,23
package y2019.Algorithm.LinkedList.medium; import y2019.Algorithm.LinkedList.ListNode; /** * @Projec ...
随机推荐
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- LNMP环境搭建——PHP篇
一.源代码安装 1.编译安装 ./configure --prefix=/usr/local/php\ --with-config-file-path=/usr/local/php/etc --wit ...
- 小贝_mysql sql语句优化过程
sql语句优化 一.SQL优化的一般步骤 (1).通过show status命令了解各种SQL的运行频率. (2).定位运行效率较低的SQL语句-(重点select) (3).通过explain分析低 ...
- ChromeDriver only supports characters in the BMP
ChromeDriver only supports characters in the BMP
- 【Ubuntu】无法挂载磁盘
我的电脑分了三个分区,A,B,C,其中A和B是Windows盘,C是ubuntu系统盘 某日发现A ,B盘没法进入了,在文件管理器中点一下,没有反应.于是右击盘符,点击挂载,跳出错误信息: (划重点) ...
- touch事件的分发机制
作者:谢昆 一段伪代码反应整个touch事件的分发 public boolean dispatchTouchEvent(MotionEvent event) { boolean consume = f ...
- 【Hnoi2010】Bzoj2002 Bounce & Codevs2333 弹飞绵羊
Position: http://www.lydsy.com/JudgeOnline/problem.php?id=3143 http://codevs.cn/problem/2333/ Descri ...
- Android 体系结构介绍
转自:http://blog.sina.com.cn/s/blog_4bc996c40100fawo.html 第一.操作系统层(OS)第二.各种库(Libraries)和Android 运行环境(R ...
- Django - 自定义请求头
收藏一下以后学习 博客搬运地址 Django接收自定义http header(转)
- J20170916-hm
スタイルシート 样式表 シール 封条 シート 纸片 マニフェスト 货单(Rails) ダイジェスト 消化,(Rails 附加哈希值) インタプリタ n. 解释者; 口译译员; [军事] 判读员; [自 ...