【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 ...
随机推荐
- [C++] 自己主动关闭右下角弹窗
近期腾讯.迅雷等各种client,都越发喜欢在屏幕的右下角弹框了. 有骨气的人当然能够把这些软件卸载了事,可是这些client在某些情况下却又还是实用的.怎么办呢? 作为码农,自己实现一个自己主动关闭 ...
- 输入两个整数n 和m,从数列1,2,3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来
中兴面试题之中的一个.难度系数中. 题目描写叙述例如以下:输入两个整数n 和m,从数列1,2.3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来. 逻辑分析: 1.比 ...
- Hibernate——三种状态的理解
在Hibernate中有三种状态,对这三种状态的深入的理解,能够更好的理解Hibernate的执行机制. 在整个Hibernate中这三种状态是能够进行转换的. 1.Transient Object( ...
- Project Euler:Problem 42 Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- Android与IOS异同点对照(1)------ 显示
Android与IOS作为当前世界上最流行的两大移动端操作系统,都拥有无数的粉丝. 那么对于Android或者IOS的开发人员来说,这两个系统都拥有什么同样或者不同的地方那?如今让我们一起来了解一下A ...
- ios7--UIImageView
// // ViewController.m // 03-UIImageView的使用 // #import "ViewController.h" @interface ViewC ...
- uboot流程分析--修改android启动模式按键【转】
本文转载自:http://blog.csdn.net/dkleikesa/article/details/9792747 本人用的android平台用的bootloader用的是uboot,貌似大多数 ...
- ubuntu安装ruby,安装sass,安装compass
安装ruby apt-get install ruby. 默认会安装1.9的. 不是自己想要的. 可以进行下面的处理. # sudo apt-get install python-software-p ...
- RxJava使用介绍
主讲人:阳石柏 RxJava基本概念 背压概念介绍 RxJava 2.0版本介绍及更新 一.RxJava基本概念 RxJava 在 GitHub 主页上的自我介绍是 “a library for co ...
- Bing Maps进阶系列八:在Bing Maps中集成OpenStreetMap地图
Bing Maps进阶系列八:在Bing Maps中集成OpenStreetMap地图 OSM(OpenStreetMap-开放街道地图)服务就是一种发布自己地图数据图片为服务的一种实现类型,开放街道 ...