每日一题 力扣 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 著作权归领扣网络所有 ...
随机推荐
- .NET中使用Redis总结——2.项目实战
接上篇.NET中使用Redis总结 -- 1.Redis搭建 看一些Redis相关资料,.NET 方面ServiceStack.Redis 用的比较多,就直接拿来用了. 在使用过程中经常过出现假死状态 ...
- linux 安装 node 和 npm 服务
1.安装文件下载 下载地址:https://nodejs.org/zh-cn/download/ 2.安装步骤 1.将安装包上传到指定位置(我习惯放到:/usr/local/application/目 ...
- Java Stream常见用法汇总,开发效率大幅提升
本文已经收录到Github仓库,该仓库包含计算机基础.Java基础.多线程.JVM.数据库.Redis.Spring.Mybatis.SpringMVC.SpringBoot.分布式.微服务.设计模式 ...
- 联系我们html代码
Syntor by Aceto 11 Boleyn Court, Manor Park, Runcorn, Chesire WA7 1SR +44 (0) 1928 579865 + 44 (0) 1 ...
- C++ 基于libbfd实现二进制加载器
构建工具解析二进制文件,基于libbfd实现,提取符号和节 BFD库 文档参考: LIB BFD, the Binary File Descriptor Library BFD及Binary File ...
- Redis 报”OutOfDirectMemoryError“(堆外内存溢出)
Redis 报错"OutOfDirectMemoryError(堆外内存溢出) "问题如下: 一.报错信息: 使用 Redis 的业务接口 ,产生 OutOfDirectMemor ...
- 第三方接口调用httpUtils
1.GET 请求 public static JSONObject getHttpGetResp(String url, String authorization, String title) { H ...
- shell基本命令与参数
1:一行可以有多个命令,用";"分开 如: cd ..; ls -l 2:先项用"-"开始,多个连接可连在一起,如:ls -lh, 3:"--&quo ...
- Prism Sample 7 Modules Code
例7对注册Module使用了配置命令. 见app.xaml.cs: 1 using Modules.Views; 2 using Prism.Ioc; 3 using Prism.Modularity ...
- dos命令、变量、字符编码、注释、用户输入
一.dos命令 1.dos命令 c: 切换盘符 cd c:\pthon 切换路径 dir 查看目录下的文件 cd .. 返回到上一层目录 cd ../.. 返回到上一层的上一层目录 二.环境变量的配置 ...