题目

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.

解析

题目的意思是输入两个链表,将两个链表的值求和获得一个新的链表。
在拿到这个题目的第一反应是将两个链表分别转换为int数值,例如(2->4->3)转换为342,然后求和之后获得807,再将807重新解析成(7->0->8)。
这种解法在大多数的情况下是没有问题的,但是遇到一些非常极端的输入时会出现问题。例如在输入(9->9->9....x1000个9),(0)的时候,第一个链表转换出来的值明显超出了java基本类型的范围了,那么这时就不能采取这种先求值再求和的方法了。
那么很明显了,题目的目的是考链表的循环以及链表的构造,那么循环两个链表的同时将节点的值求和放入结果链表中,有进位则记录,在下次循环时进位即可:

public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {if(l1 == null || l2 ==null){return null;}ListNode first = null ,next = null;int plus = 0;while(l1!=null || l2!=null || plus!=0){int num1 = l1==null? 0 : l1.val;int num2 = l2==null? 0 : l2.val;int sum = num1 + num2 +plus;if(sum>=10){sum-=10;plus=1;}else{plus=0;}if(first==null){first = new ListNode(sum);next = first;}else{next.next = new ListNode(sum);next = next.next;}if(l1!=null && l1.next!=null){l1 = l1.next;}else{l1 = null;}if(l2!=null && l2.next!=null){l2 = l2.next;}else{l2 = null;}}return first;}

以上代码经过简化以及改进后可以得出以下代码:

public static ListNode addTwoNumbersBetter(ListNode l1, ListNode l2) {ListNode fakeHead = new ListNode(0);ListNode curr = fakeHead;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;curr.next = new ListNode(sum%10);curr = curr.next;if(l1!=null) l1 = l1.next;if(l2!=null) l2 = l2.next;}if(carry>0){curr.next = new ListNode(carry);}return fakeHead.next;}

Leecode-2 Add Two Numbers相关推荐

  1. LeetCode 445. Add Two Numbers II--面试算法题--C++,Python解法

    题目地址:Add Two Numbers II - LeetCode You are given two non-empty linked lists representing two non-neg ...

  2. [Leetcode] 445. Add Two Numbers II

    问题: https://leetcode.com/problems/add-two-numbers-ii/#/description 思路:该题与"415. Add Strings" ...

  3. LeetCode之Add Two Numbers

    LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...

  4. LeetCode刷题实战(2):Add Two Numbers

    题2描述: 2 Add Two Numbers 29.10% Medium You are given two non-empty linked lists representing two non- ...

  5. leetcode算法—两数相加 Add Two Numbers

    关注微信公众号:CodingTechWork,一起学习进步. 题目 Add Two Numbers: You are given two non-empty linked lists represen ...

  6. leetcode(二)—— Add Two Numbers(Python/C++)

    Add Two Numbers | LeetCode OJ 逻辑是简单的,主要是处理进位,以及两序列长度不一致的情况: Python # 链表结点定义 class ListNode(object):d ...

  7. LeetCode题解:Add Two Numbers

    LeetCode题解 说明:本人不是什么算法高手,部分内容参考了Google.stackoverflow.segmentfault中得到的一些解答或者代码.之所以去做Leetcode上的题是因为毕业工 ...

  8. LeetCode 445. Add Two Numbers II

    LeetCode 445. Add Two Numbers II Solution1:我的答案 利用了栈,这样就不用翻转链表了... /*** Definition for singly-linked ...

  9. 【注意】LeetCode 2. Add Two Numbers

    LeetCode 2. Add Two Numbers 这种沙比提怎么都写不对了??? Solution1:学习这种写法 /*** Definition for singly-linked list. ...

  10. LeetCode久不久来一题系列之Add Two Numbers

    题目来源: https://leetcode.com/problems/add-two-numbers/description/ 题目: Add Two Numbers 题目描述: You are g ...

最新文章

  1. 继承log4.net的类
  2. 2018牛客网暑假ACM多校训练赛(第二场)E tree 动态规划
  3. ABAP:ALV List报表
  4. 在Putty或mRemote下输入和显示中文
  5. spring5源码解读
  6. java界面 ppt_Java GUI图形用户界面 课件.ppt
  7. 镜像浏览器_害怕win10镜像有第三方软件,直接到微软官网下载,原汁原味
  8. 【二分图】【最大匹配】【匈牙利算法】bzoj1191 [HNOI2006]超级英雄Hero
  9. iMazing for mac中文版苹果iOS设备管理器(已更新至v2.9.12版本)
  10. 计算机操作系统-详细版-王道
  11. 3.关于运动控制芯片
  12. 使用ADB安装Apk到手机
  13. 示波器电流探头应该怎么保养-PinTech品致
  14. 在出境通关中如何应用智能智慧护照阅读器技术呢
  15. 手游辅助制作原理和学习思路
  16. CTP常见问题系列之一 “CTP : 不合法的登录“
  17. 蓝桥杯项目一:可调闹钟1(详解版)
  18. 3款大屏播放软件,用法很详细,用于大屏幕播放视频和图片
  19. 看程序员奶爸是如何通过代码给宝宝起名的~
  20. 微信小程序中使用vant组件库(超详细)

热门文章

  1. (亲测无坑)Centos7.x使用kubeadm安装K8s集群1.15.0版本
  2. shell脚本——释放缓存
  3. CentOS新增硬盘系统不能自动进行识别。
  4. MegaCli常见命令
  5. 命令行中,变量 date time 格式化设定
  6. 【Hive】动态分区插入
  7. 连接DB2 抛异常SQL Error SQLCODE=-204, SQLSTATE=42704
  8. QT学习之解决QT中QIcon图标不显示的问题
  9. 微服务架构的分布式事务解决方案(Dubbo分布式事务处理)
  10. ubuntu18.04 没声音解决方案(坑自己版)