可以直接用栈去做就行,逆序想到栈的做法

然后算完一个就直接赋值给答案数组

 我用的是常见

  1. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  2.  
  3. int sizeA=0;
  4. int sizeB=0;
  5.  
  6. ListNode start=l1;
  7. ListNode r1=l1;
  8. ListNode r2=l2;
  9. while (start!=null){
  10. r1=start;
  11. start=start.next;
  12. sizeA++;
  13. }
  14.  
  15. start=l2;
  16. while (start!=null){
  17. r2=start;
  18. start=start.next;
  19. sizeB++;
  20. }
  21.  
  22. // 逆转
  23. reverse(l1, 0);
  24. reverse(l2, 0);
  25.  
  26. int add = 0;
  27. //把长的放到r1
  28. if (sizeA<sizeB){
  29. ListNode t=r1;
  30. r1=r2;
  31. r2=t;
  32. }
  33.  
  34. start=r1;
  35. while (r1 != null ) {
  36.  
  37. int a = r1.val;
  38. int b = r2 == null ? 0 : r2.val;
  39.  
  40. // 全都放到r1上面
  41. r1.val = (a + b + add) % 10;
  42. add = (a + b + add) / 10;
  43. r1=r1.next;
  44. if (r2!=null){
  45. r2=r2.next;
  46. }
  47. }
  48. //返回头节点
  49. // 对start逆转
  50. ListNode[] reverse = reverse(start, 0);
  51. if (add!=0){
  52. ListNode temp = new ListNode(add);
  53. temp.next=reverse[1];
  54. return temp;
  55. }
  56.  
  57. return reverse[1];
  58.  
  59. }
  60.  
  61. /**
  62. * 返回2个
  63. * 0 下一个
  64. * 1 尾巴
  65. * @param root
  66. * @param sum
  67. * @return
  68. */
  69. public ListNode[] reverse(ListNode root, int sum) {
  70.  
  71. if (root == null) {
  72. return null;
  73. }
  74. sum++;
  75. ListNode[] res = reverse(root.next, sum);
  76. ListNode next=null;
  77. if (res!=null){
  78. next=res[0];
  79. }
  80. if (next != null) {
  81. root.next=null;
  82. next.next = root;
  83.  
  84. //返回尾巴
  85. return new ListNode[]{root,res[1]};
  86. } else {
  87. return new ListNode[]{root,root};
  88. }
  89.  
  90. }

的做法,将链表进行逆转,适合大部分情况

每日一题 力扣 445 https://leetcode.cn/problems/add-two-numbers-ii/的更多相关文章

  1. LeetCode 题解之Add Two Numbers II

    1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { Lis ...

  2. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  3. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

  4. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  5. 445. Add Two Numbers II - LeetCode

    Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...

  6. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  7. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

  8. 445 Add Two Numbers II 两数相加 II

    给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...

  9. 刷题-力扣-剑指 Offer 42. 连续子数组的最大和

    剑指 Offer 42. 连续子数组的最大和 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de ...

  10. 刷题-力扣-剑指 Offer II 055. 二叉搜索树迭代器

    剑指 Offer II 055. 二叉搜索树迭代器 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kTOapQ 著作权归领扣网络所有 ...

随机推荐

  1. day26:装饰器&面向对象当中的方法&property

    目录 1.装饰器 1.1 装饰器的基本用法 1.2 @符号的使用 1.3 装饰器的嵌套 1.4 用装饰器扩展带有参数的原函数 1.5 用装饰器扩展带有参数和返回值的原函数 1.6 用类装饰器扩展原函数 ...

  2. Vue修改单页面背景颜色

  3. Docker MariaDB配置主从复制

    编写主节点配置文件master.cnf: [client] # 默认字符集 default-character-set=utf8mb4 [mysqld] # 字符集 character-set-ser ...

  4. Docker Compose 部署 Jenkins

    Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具 Jenkins应用广泛,大多数互联网公司都采用Jenkins配合GitLab.Docker.K8s作为实现D ...

  5. 基于Java实现数据脱敏

    用法 Jdk版本 大于等于1.8 maven依赖 <dependency> <groupId>red.zyc</groupId> <artifactId> ...

  6. Nuxt3环境变量配置

    Nuxt3 正式发布还不到半年,在投入生产环境使用后,遇到了不少问题,很难找到合适的解决方案,其中环境变量配置就是其中一个,之前一直未能解决,最近要上持续集成,无法绕过这个问题,所以花了点时间研究了一 ...

  7. Layui+dtree实现左边分类列表,右边数据列表

    效果如下 代码实现 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  8. springboot 项目国际化+登录拦截器

    项目页面国际化 1.语言配置文件 需要下载插件Resource Bundle Editor 新建国际目录i18n 在properties配置文件中自定义 2.前端index页面要设置语言参数传递给后端 ...

  9. vscode 配置代码自动格式化加修复

    子曰:"工欲善其事,必先利其器", 编码必须的就是有一个顺手的ide,然而光有还不行,还要懂得配置,毕竟不同的团队代码规范不同,如目前用得较多的就是eslint,今天就顺便记录下v ...

  10. weex 中出现 loading无法关闭

    如题使用weex 搞个app 一安装就有一个bug 一直这里转!!! 找了半天原来是自己没按规定来,在index.vue中直接使用了rower <template> <router- ...