题目:
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

翻译:
给你2个链表,代表2个非负整数。链表中整数的每一位数字的存储是反序的,数组的每个节点都包含一个数字。把2个非负整数相加,并且用一个链表返回。
输入: (2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 0 -> 8

分析:
由于刚好链表中数的存储是反序,也就是个位在最前面,正好方便我们从低位开始加。需要注意的几个点:
1. 传入[][],也就是2个空链表,要返回null
2. 传入[0][0],也就是2个整数是0的处理,要返回[0]
3. 传入[5][5],要新增一个节点,存储进位。也就是判断是否结束,要根据2个链表是否为空和是否有进位来判断。

Java版代码:

/*** 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;}ListNode head=new ListNode(0);ListNode currentNode=head;int fix=0;while(l1!=null||l2!=null||fix!=0){int val1=0;int val2=0;if(l1!=null){val1=l1.val;l1=l1.next;}if(l2!=null){val2=l2.val;l2=l2.next;}int sum=val1+val2+fix;fix=0;if(sum>9){fix=1;sum-=10;}currentNode.next=new ListNode(0);currentNode.next.val=sum;currentNode=currentNode.next;}if(head.next==null){head.next=new ListNode(0);}return head.next;}
}

Leet Code OJ 2. Add Two Numbers [Difficulty: Medium]相关推荐

  1. Leet Code OJ 260. Single Number III [Difficulty: Medium]

    题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  2. Leet Code OJ 482. License Key Formatting [Difficulty: Medium]

    题目 Now you are given a string S, which represents a software license key which we would like to form ...

  3. Leet Code OJ 258. Add Digits [Difficulty: Easy]

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  4. Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  5. Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]

    题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...

  6. Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  7. Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  8. Leet Code OJ 326. Power of Three [Difficulty: Easy]

    题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...

  9. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

最新文章

  1. jquery插件制作
  2. python表示数字6_【Python 1-6】Python教程之——数字
  3. 利用OpenCV的Grabcut()函数实现图像的前景与背景的分割-并对Grabcut()作详细介绍
  4. myloader原理解析
  5. 蓝桥杯java最小公倍数_蓝桥杯算法训练 最大最小公倍数
  6. picpick尺子像素大小精度不够准确_精度与分辨率是一回事吗
  7. ttribute value is quoted with which must be escaped when used within the value
  8. python getattr_Python 内置方法和属性应用:反射和单例
  9. Java并发性和多线程介绍目录
  10. mysql 读取文件_关于mysql:逐行读取文件而不将整个文件加载到内存中
  11. 测试用例集-9.QQ登录功能测试用例
  12. ValueError: Object arrays cannot be loaded when allow_pickle=False 报错解决
  13. apriori关联规则
  14. 串口通信(232,485,422)以及常见问题
  15. win10系统QQ音乐安装包无法打开解决方法!
  16. Url Rewrite Filter 使用全攻略
  17. 【大话设计模式-2】UML 类图的绘制(源码案例分析)
  18. SQLServer中的N是什么意思?
  19. android x86主动防御,LBE安全大师(主动式防御软件) for Android v6.1.2235 官网版 中文官方安装版...
  20. uniapp 中 Cannot read property ‘length‘ of undefined 报错处理

热门文章

  1. 逆向工程核心原理读书笔记-API钩取之记事本小写转大写
  2. c++ 继承机制易犯的错误
  3. 交换机网络嗅探方法之用ARP欺骗辅助嗅探
  4. JDBC连接失败java.sql.SQLException: ...ClassCastException: BigInteger cannot be cast to Long
  5. 一个奇葩的网络问题,把技术砖家搞蒙了
  6. TCP/IP协议栈到底是内核态好还是用户态好?
  7. 狗屎一样的代码如何重构?
  8. k8s 为何成为大厂标配?
  9. 査勇:华为云在视频AI转码领域的技术实践
  10. LiveVideoStack线上分享第五季(十二):移动端多种通话场景统一技术方案实践...