2019独角兽企业重金招聘Python工程师标准>>>

Add Two Numbers

题目

Total Accepted: 137297  Total Submissions: 599150  Difficulty: Medium

You are given two linked lists representing two non-negative numbers. 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.

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

Output: 7 -> 0 -> 8

Subscribe to see which companies asked this question

解题思路

任意选择一个Node进行迭代,迭代中有几种可能 1、Node1个数少于Node2 2、Node1个数和Node2相等 3、Node1个数大于Node2 分别进行考虑,计算进位和取模,迭代过程中改变Node1及Node2的偏移量,然后从当前偏移量Node2开始Node2的迭代。

代码

    /*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) { val = x; }* }*/public class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {if(l1==null||l2==null)return null;int multiple=0;ListNode result=null,nextNode=null;//用来表示结果与下一个Nodewhile(l1!=null){int n1=0,n2=0,sum=0;n1= l1.val;if(l2!=null){n2= l2.val;sum=n1+n2;sum+=multiple;multiple=0;if(sum>9){multiple=sum/10;sum=sum%10;}if(result==null){result=new ListNode(sum);nextNode=result;}else{ nextNode.next=new ListNode(sum);nextNode=nextNode.next;}l2=l2.next;//l2的指针随l1偏移l1=l1.next;}else{n1+=multiple;multiple=0;if(n1>9){multiple=n1/10;n1=n1%10;}nextNode.next=new ListNode(n1);nextNode=nextNode.next;l1=l1.next;}}while(l2!=null){//如果l1链表长度少于l2链表长度int n2= l2.val;n2+=multiple;multiple=0;if(n2>9){multiple=n2/10;n2=n2%10;}nextNode.next=new ListNode(n2);nextNode=nextNode.next;l2=l2.next;}if(multiple!=0){//迭代完Node1、Node2 最后如果进位不为0 则加一位nextNode.next=new ListNode(multiple);nextNode=nextNode.next;}return result;}}

复杂度

O(n)的时间 O(n)的内存

测试结果

其他思路

后面看到有用递归做这个题目的思路,代码会精简很多,大家可以尝试下

注:版权所有转载请注明出处http://my.oschina.net/ambitor/blog/662824,作者:Ambitor

转载于:https://my.oschina.net/ij5IYLKW/blog/662824

Leetcode(二):Add Two Numbers相关推荐

  1. LeetCode之Add Two Numbers

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

  2. LeetCode 445. Add Two Numbers II

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

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

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

  4. 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 ...

  5. [Leetcode] 445. Add Two Numbers II

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

  6. LeetCode:2. Add Two Numbers

    https://leetcode.com/problems/add-two-numbers/description/ 内容描述 You are given two non-empty linked l ...

  7. LeetCode 2. Add Two Numbers

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

  8. leetcode 【 Add Two Numbers 】 python 实现

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  9. leetcode 2. Add Two Numbers | 2. 两数相加(Java)

    题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 需要注意 当两个链表不一样长时 的收尾处理. /*** Definition for ...

  10. Leetcode: 2. Add Two Numbers

    一直想用一个进位变量来存贮进位值,但老是考虑不周全,下面是我自己写的bug代码,考虑不周,因为l1或者l2都有可能为null class Solution {public ListNode addTw ...

最新文章

  1. win10 mbr下装linux,在Win10下安装Ubuntu 18.04双系统(MBR+Legacy)
  2. php如何批量导入题库,批量文本导入试题
  3. 多媒体制作技术心得体会_论多媒体教学的改革与创新
  4. 洛谷 P4430 小猴打架
  5. vs2019创建android,Visual Studio 2019编写的安卓程序,生成失败
  6. 考研计算机考试408背诵,今年上岸计算机准研究生分享一下计算机考研(408)经验...
  7. 果蔬连连看java源代码_《基于Qt的连连看游戏的设计》-毕业论文.doc
  8. android job service,服务保活那些事(Job Service ,JobScheduler)
  9. Winsock—I/O模型之事件选择模型(一)
  10. Python 在图片加上消息通知的文字
  11. z-buffer的概念和算法
  12. 工程量清单软件测试,输变电工程量清单投标标准化应用软件BQB2017基本操作方法...
  13. ROS实战篇(一)如何在ROS中编写自己的package?------ 以节点通信为例
  14. H.265视频编码原理总结
  15. web前端面试学习指南
  16. ballerina 学习二十七 项目k8s部署 运行
  17. Node 裁切图片的方法
  18. 要闻君说:阿里云联合8家芯片模组商推出“全平台通信模组”;北汽与小桔车服联合“京桔”;IBM要帮助印度公司打造5G战略!...
  19. 分词工具与方法:jieba、spaCy等
  20. JavaScript -- ajax相关知识点的笔记

热门文章

  1. 天然气阶梯是按年还是按月_您搞懂了吗?阶梯电费是按年计算而不是按月计算的...
  2. oracle中job的retry次数,Oracle 19c注意事项: DBMS_JOB 行为变化
  3. 线程池原理_Java线程池实现原理
  4. 网络摄像头ip php,C#实现IP摄像头的方法
  5. bp神经网络数字识别matlab_基于Matlab的BP神经网络识别26个英文字母
  6. python语言中print函数的作用_[转载]python中的@符号的作用
  7. php读取mssql中文乱码,PHP读取mssql json数据中文乱码的解决办法
  8. 每日自增字段mysql_2020-11-05 触发器实现mysql每日自增字段
  9. synchronized的作用以及不足
  10. 关于前端模块化你应该知道的