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

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. 【Python】词典

    词典 (dictionary).与列表相似,词典也可以储存多个元素.这种储存多个元素的对象称为容器(container) 基本概念 常见的创建词典的方法: >>>dic = {'to ...

  2. cassandra压力测试

    http://docs.datastax.com/en/archived/cassandra/2.2/cassandra/tools/toolsCStress.html?hl=stress Simpl ...

  3. 【sparkStreaming】SparkStream的创建

    DStream编程数据模型 DStream(Discretized Stream)作为Spark Streaming的基础抽象,它代表持续性的数据流. 这些数据流既可以通过外部输入源赖获取,也可以通过 ...

  4. Selenium-xapth定位

    xpath 的定位方法, 非常强大.  使用这种方法几乎可以定位到页面上的任意元素. 什么是xpath xpath 是XML Path的简称, 由于HTML文档本身就是一个标准的XML页面,所以我们可 ...

  5. SpringBoot_11_将springboot项目部署到外部tomcat上

    一.前言 二. 三.参考资料 如何将Spring Boot项目打包部署到外部Tomcat 2.SpringBoot 项目如何在tomcat容器中运行

  6. 【python】windows下安装xgboost的python库

    傻瓜教程 主要参考了https://www.hongweipeng.com/index.php/archives/826/  和 https://github.com/dmlc/xgboost/iss ...

  7. js常用算法

    1.判断一个字符串是“回文”类型,回文:形如‘abcba’.‘mamam’这种第一个与最后一个字符相同,第二个和倒数第二个字符相同...一次类推,该怎么实现呢? 对与我来说,首先看到第一眼,还真没想起 ...

  8. c printf打印格式

    关于小数点位数的举例:  <pre lang="c" escaped="true">#include <stdio.h> /* 当fah ...

  9. jQuery学习_具备吸附功能的拖曳框

    在线演示:http://sandbox.runjs.cn/show/2drrwkrx 关键点:保持一个不变,鼠标拖动时与边框的距离 === 鼠标左击时与边框的距离 源码: <!DOCTYPE h ...

  10. HihoCoder 1104 : Suzhou Adventure(树形DP)

    Suzhou Adventure 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is taking an adventure in Suzhou n ...