每日一题 力扣 445 https://leetcode.cn/problems/add-two-numbers-ii/
可以直接用栈去做就行,逆序想到栈的做法
然后算完一个就直接赋值给答案数组
- public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
- int sizeA=0;
- int sizeB=0;
- ListNode start=l1;
- ListNode r1=l1;
- ListNode r2=l2;
- while (start!=null){
- r1=start;
- start=start.next;
- sizeA++;
- }
- start=l2;
- while (start!=null){
- r2=start;
- start=start.next;
- sizeB++;
- }
- // 逆转
- reverse(l1, 0);
- reverse(l2, 0);
- int add = 0;
- //把长的放到r1
- if (sizeA<sizeB){
- ListNode t=r1;
- r1=r2;
- r2=t;
- }
- start=r1;
- while (r1 != null ) {
- int a = r1.val;
- int b = r2 == null ? 0 : r2.val;
- // 全都放到r1上面
- r1.val = (a + b + add) % 10;
- add = (a + b + add) / 10;
- r1=r1.next;
- if (r2!=null){
- r2=r2.next;
- }
- }
- //返回头节点
- // 对start逆转
- ListNode[] reverse = reverse(start, 0);
- if (add!=0){
- ListNode temp = new ListNode(add);
- temp.next=reverse[1];
- return temp;
- }
- return reverse[1];
- }
- /**
- * 返回2个
- * 0 下一个
- * 1 尾巴
- * @param root
- * @param sum
- * @return
- */
- public ListNode[] reverse(ListNode root, int sum) {
- if (root == null) {
- return null;
- }
- sum++;
- ListNode[] res = reverse(root.next, sum);
- ListNode next=null;
- if (res!=null){
- next=res[0];
- }
- if (next != null) {
- root.next=null;
- next.next = root;
- //返回尾巴
- return new ListNode[]{root,res[1]};
- } else {
- return new ListNode[]{root,root};
- }
- }
的做法,将链表进行逆转,适合大部分情况
每日一题 力扣 445 https://leetcode.cn/problems/add-two-numbers-ii/的更多相关文章
- LeetCode 题解之Add Two Numbers II
1.题目描述 2.分析 首先将链表翻转,然后做加法. 最后将结果链表翻转. 3.代码 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { Lis ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)
https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...
- 445 Add Two Numbers II 两数相加 II
给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
- 刷题-力扣-剑指 Offer 42. 连续子数组的最大和
剑指 Offer 42. 连续子数组的最大和 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de ...
- 刷题-力扣-剑指 Offer II 055. 二叉搜索树迭代器
剑指 Offer II 055. 二叉搜索树迭代器 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kTOapQ 著作权归领扣网络所有 ...
随机推荐
- day26:装饰器&面向对象当中的方法&property
目录 1.装饰器 1.1 装饰器的基本用法 1.2 @符号的使用 1.3 装饰器的嵌套 1.4 用装饰器扩展带有参数的原函数 1.5 用装饰器扩展带有参数和返回值的原函数 1.6 用类装饰器扩展原函数 ...
- Vue修改单页面背景颜色
- Docker MariaDB配置主从复制
编写主节点配置文件master.cnf: [client] # 默认字符集 default-character-set=utf8mb4 [mysqld] # 字符集 character-set-ser ...
- Docker Compose 部署 Jenkins
Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具 Jenkins应用广泛,大多数互联网公司都采用Jenkins配合GitLab.Docker.K8s作为实现D ...
- 基于Java实现数据脱敏
用法 Jdk版本 大于等于1.8 maven依赖 <dependency> <groupId>red.zyc</groupId> <artifactId> ...
- Nuxt3环境变量配置
Nuxt3 正式发布还不到半年,在投入生产环境使用后,遇到了不少问题,很难找到合适的解决方案,其中环境变量配置就是其中一个,之前一直未能解决,最近要上持续集成,无法绕过这个问题,所以花了点时间研究了一 ...
- Layui+dtree实现左边分类列表,右边数据列表
效果如下 代码实现 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- springboot 项目国际化+登录拦截器
项目页面国际化 1.语言配置文件 需要下载插件Resource Bundle Editor 新建国际目录i18n 在properties配置文件中自定义 2.前端index页面要设置语言参数传递给后端 ...
- vscode 配置代码自动格式化加修复
子曰:"工欲善其事,必先利其器", 编码必须的就是有一个顺手的ide,然而光有还不行,还要懂得配置,毕竟不同的团队代码规范不同,如目前用得较多的就是eslint,今天就顺便记录下v ...
- weex 中出现 loading无法关闭
如题使用weex 搞个app 一安装就有一个bug 一直这里转!!! 找了半天原来是自己没按规定来,在index.vue中直接使用了rower <template> <router- ...