【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 ...
随机推荐
- VC中CString和WPARAM之间的相互转换
在传递自己定义消息的过程中.须要转换CString 变量. 在发送消息端使用例如以下方法: SendMessage(WM_MESSAG_MINE,0,(LPARAM)strVal.AllocSysSt ...
- 2015南阳CCPC H - Sudoku 数独
H - Sudoku Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny g ...
- 行政区划代码(JSON版本)2018年8月
字段:regioncode //行政区划代码 regionname //行政区划名称 pcode //行政区划上一级代码 [{ "REGIONCODE": "11000 ...
- hbase查询_Phoenix及hbase repl命令行两种方式
一.Phoenix(jdbc)登陆 1.cd /home/mr/phoenix/bin(此路径每个环境里面有可能不一样)2../sqlline.py localhost 二.shell repl Hb ...
- Android Studio报错:DefaultAndroidProject : Unsupported major.minor version 52.0
今天使用Android Studio 2.0打开我之前的项目时,编译报了如下错误: Error:Cause: com/android/build/gradle/internal/model/Defau ...
- Gym - 100162G 2012-2013 Petrozavodsk Winter Training Camp G. Lyndon Words 暴力枚举
题面 题意:如果一个字符串的最小表示法是他自己,他就是一个Lyndon Word. 例如 aabcb 他的循环串有 abcba bcbaa cbaab baabc 其中字典序最小的是他自己 现在给 ...
- Java命名规范(新手宝典)
很多刚开始学习Java的童鞋都不知道如何命名类文件,方法名,字段名,常量名等,今天抽出时间整理了了一下.大佬绕过 Java命名的组成规则:英文大小写字母,数字,$和_. 这里有几点需要注意: 不能以数 ...
- javascript中window,document,body的解释
解释javascript中window,document,body的区别: window对象表示浏览器中打开的窗口,即是一个浏览器窗口只有一个window对象. document对象是载入浏览器的ht ...
- 像素缓冲区对象PBO 记录
像素缓冲区对象PBO 记录 和所有的缓冲区对象一样,它们都存储在GPU内存中,我们可以访问和填充PBO,方法和其他的缓冲区一样. 当一个PBO被绑定到GL_PIXEL_PACK_BUFFER,任何读取 ...
- Educational Codeforces Round 24 题解
A: 考你会不会除法 //By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long long ...