public class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode dummy = new ListNode(-1);ListNode cur = dummy;int carry = 0;while (l1 != null || l2 != null) {int d1 = l1 == null ? 0 : l1.val;int d2 = l2 == null ? 0 : l2.val;int sum = d1 + d2 + carry;carry = sum >= 10 ? 1 : 0;cur.next = new ListNode(sum % 10);cur = cur.next;if (l1 != null) l1 = l1.next;if (l2 != null) l2 = l2.next;}if (carry == 1) cur.next = new ListNode(1);return dummy.next;}
}

转载于:https://www.cnblogs.com/wangkun1993/p/6352265.html

leetcode 2 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. 我是买家的前世今生,该到了say goodbye的时候了!
  2. Nginx +uwsgi+django配置
  3. (Java多线程)多线程原理和使用
  4. 【数据结构与算法】【算法思想】【算法总结】索引结构
  5. MATLAB高光谱图像构建KNN图
  6. html lineheight div,html – Chrome上的文本输入:line-height似乎有最小值
  7. Linux查找大文件 (find的用法)
  8. 理解各种聚类算法的差异
  9. JQ树形菜单加表格混合使用:treeTable组件使用
  10. 对比(5.1.48VS8.0.16)getConnection、isValid
  11. 模仿企鹅FM播放主页面滑动动态改变各视图的大小
  12. 2021双非计算机保研推免经验分享——海王养成系列(一)
  13. 某某酒店维修工作记录------弱电篇
  14. 【板绘手绘线稿素材资料】从素描到板绘,超全的人物嘴巴画法!
  15. XGBoost feature importance特征重要性-实战印第安人糖尿病数据集
  16. 推荐一些经典的炒股书籍
  17. 基于STM8S003f3的单键触摸开关制作
  18. mycat分库分表配置
  19. 将系统(deepin15.11)装进U盘里面,在任何电脑上即插即用,使用自己的系统
  20. 山东大学计算机学院预推免,【更新】2021届计算机相关预推免信息(个人使用)...

热门文章

  1. 操作系统(十五)调度算法的评价指标
  2. C语言再学习 -- Linux下find命令用法
  3. python如何判断字典中是否存在某个键_总结:11个Python3字典内置方法大全及示例...
  4. 汇编语言LENGTHOF运算符:计算数组中元素的个数
  5. Android VNC Server
  6. android app通过Geth RPC接口实现远程调用
  7. NEO versus Ethereum: Why NEO might be 2018’s strongest cryptocurrency
  8. Android平台类加载流程源码分析
  9. Sparkmllib scala逻辑回归
  10. c语言选课系统课程设计,用C语言写一个 学生选课系统