题目大意:使用链表表示的两个整数,计算出其和,以同样的形式返回。

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

觉得做OJ最大的问题是有时看不懂题目。

其实是这样的,一个链表代表一个非负整数数,是从右往左看的:

(2 -> 4 -> 3) = 342;

(5 -> 6 -> 4)=465;

342+465=7 -> 0 -> 8=807;

逆序其实是很大很大的便利!!!!

因为最左边是个位,只要这样一个个加,在考虑进位就好了。而且个位数相加,进位只能是1.

如果左边是最高位就麻烦了,因为数都是从最低位开始加的,还要遍历到最右边什么的。

所以思路如下:

1、一个用来记录进位的变量

2、如果两个链表都非空,把结点得数取出来,相加,对10取余构造新结点,记录进位。list=list.next

3、如果任意一个链表空了,就直接以他的结点构造新结点。

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

ListNode temp1 = l1;
ListNode temp2 = l2;

int temp =temp1.val+temp2.val;

//System.out.println("first------->"+temp);

int jinwei = 0;

if(temp>=10){

temp%=10;
jinwei=1;

}

temp1=temp1.next;
temp2=temp2.next;

ListNode result = new ListNode(temp);
ListNode resulttemp = result;

while(temp1!=null&&temp2!=null){

//连个链表都有结点,则两节点相加
temp =temp1.val+temp2.val;

//如果前面有进位
if(jinwei==1){

temp++;
jinwei--;

}

//考虑进位

if(temp>=10){

temp%=10;
jinwei=1;

}

resulttemp.next = new ListNode(temp);

temp1=temp1.next;
temp2=temp2.next;
resulttemp=resulttemp.next;

}
while(temp1!=null){

if(jinwei==1){

int t1 = ++temp1.val;
jinwei--;

if(t1>=10){

t1%=10;
jinwei++;

}

resulttemp.next = new ListNode(t1);

}
else{

resulttemp.next = new ListNode(temp1.val);

}
resulttemp=resulttemp.next;
temp1=temp1.next;

}
while(temp2!=null){

if(jinwei==1){

int t2 = ++temp2.val;
jinwei--;

if(t2>=10){

t2%=10;
jinwei++;

}

resulttemp.next = new ListNode(t2);

}
else{

resulttemp.next = new ListNode(temp2.val);

}
resulttemp=resulttemp.next;
temp2=temp2.next;

}
if(temp1==null&&temp2==null&&jinwei==1)
resulttemp.next = new ListNode(1);

return result;

}
}

所以说,大数的构造就应该这样构造,虽然是一道加法题,但是加减乘除都能很好的支持。

一开始想的是,把数取出来,然后构造一个数,再相加,然后再构造链表就行。但这样是错误的!!!

因为传入的链表可能很长很长,用long都会溢出的超级大数!

而且,这个代码是直接在LeetCode上写的,其实也挺方便,毕竟我只用printf来调试。也就是没了代码补全而已,不过不用写main函数也好。

LeetCode题解 #2 Add Two Numbers的更多相关文章

  1. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. leetcode题解2. Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  3. LeetCode 题解之Add Two Numbers II

    1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { Lis ...

  4. LeetCode题解之Add two numbers

    1.题目描述 2.题目描述 题目思路可以参考合并单链表的思路,定义一个全局 进位标志,如果两个数值相加得到需要进位,则将进位标志置为1 . 3.代码 ListNode* addTwoNumbers(L ...

  5. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  6. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  7. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  8. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  9. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

随机推荐

  1. language model ——tensorflow 之RNN

    代码结构 tf的代码看多了之后就知道其实官方代码的这个结构并不好: graph的构建和训练部分放在了一个文件中,至少也应该分开成model.py和train.py两个文件,model.py中只有一个P ...

  2. 【枚举】【最小生成树】【kruscal】bzoj3754 Tree之最小方差树

    发现,若使方差最小,则使Σ(wi-平均数)2最小即可. 因为权值的范围很小,所以我们可以枚举这个平均数,每次把边权赋成(wi-平均数)2,做kruscal. 但是,我们怎么知道枚举出来的平均数是不是恰 ...

  3. Android 进阶9:进程通信之 AIDL 解析

    读完本文你将了解: AIDL AIDL 生成文件分析 Stub Proxy AIDL 生成的内容小结 AIDL 的使用回顾 服务端 客户端 小结 手动写个 Binder 首先是定义跨进程接口实现 II ...

  4. MySQL 的中文乱码问题终结

    中文正确 1:保存sql脚本文件 utf8 !!!!! ANSI<-->GBK 2:打开mysql窗口 SET NAMES UTF8; 每次新打开窗口执行指令 3:SOURCE d:/1. ...

  5. mac brew安装使用卸载

    (一)安装 1.浏览器打开brew.sh,进入homebrew主页.找到install homebrew 的命令: /usr/bin/ruby -e "$(curl -fsSL https: ...

  6. pgrep 和 pkill 使用小记

    在停止指定进程时,经常使用如下命令: kill `ps aux | grep -w program_name | grep -v grep | awk '{print $2}'` 使用 pgrep 和 ...

  7. mac地址常识及获取

    mac常识: 网卡地址这个概念有点混淆不清.因为实际上有两个地址,mac地址和物理地址,一般说网卡地址我是指物理地址,不知道别人怎么看?物理地址指的是网卡上的存放地址的ROM里的地址,mac地址是这块 ...

  8. NOI 2018 你的名字

    因为机房里的小伙伴都在看<你的名字.>而我不想看 所以来写了这道题... 给一个 $S$ 串,$q$ 次询问,每次一个 $T$ 串,问 $T$ 有多少没在 $S[l,r]$ 中以子串形式出 ...

  9. BZOJ - 5427:最长上升子序列 (二分&思维)

    现在给你一个长度为n的整数序列,其中有一些数已经模糊不清了,现在请你任意确定这些整数的值, 使得最长上升子序列最长.(为何最长呢?因为hxy向来对自己的rp很有信心)   Input 第一行一个正整数 ...

  10. 数据结构之最小生成树Kruskal算法

    1. 克鲁斯卡算法介绍 克鲁斯卡尔(Kruskal)算法,是用来求加权连通图的最小生成树的算法. 基本思想:按照权值从小到大的顺序选择n-1条边,并保证这n-1条边不构成回路. 具体做法:首先构造一个 ...