每日一题 力扣 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 著作权归领扣网络所有 ...
随机推荐
- day117:MoFang:宠物栏的功能实现&宠物道具的使用
目录 1.宠物栏的功能实现 2.宠物道具的使用 1.宠物栏的功能实现 1. 宠物的显示 2. 宠物的使用 3. 宠物的饱食度 4. 宠物的开锁 1.服务端提供显示宠物的api接口 orchard/so ...
- 运行项目报错Cannot read property 'styles' of undefined
原因是安装依赖版本不对,以下是我的解决办法: 1.先删除项目中package-lock.json 文件 及node_modules文件(可使用rimraf指令删除node_modules,直接删文件很 ...
- The first week match's mistake-2
旋转排列 (https://www.luogu.com.cn/problem/B3688) 解读一下题目: 要求从给定的数组拿出最后一个数字后 看看变化后的数组的最后一个数字是否是要求的数字 想到用栈 ...
- Elasticsearch搜索功能的实现(五)-- 实战
实战环境 elastic search 8.5.0 + kibna 8.5.0 + springboot 3.0.2 + spring data elasticsearch 5.0.2 + jdk 1 ...
- Vue中的$set的使用 (为对象设置属性)
data() { return { obj: { name: 'shun' } } } 对象只有name属性,通过$set给对象添加属性(三个参数,对象名,属性名, 属性) setage() { th ...
- [C++项目] 通讯录管理系统
文章目录 通讯录管理系统 1.系统需求 2.创建项目 2.1 创建项目 2.2添加文件 3.菜单功能 4.退出功能 5.添加联系人 5.1 设计联系人结构体 5.2 设计通讯录结构体 5.3 main ...
- 谁想和我一起做低代码平台!一个可以提升技术,让简历装x的项目
序言 正如文章标题所述,最近一段时间低代码这个概念非常的火,但其实在不了解这个东西的时候觉得它真的很炫酷,从那时就萌生了做一个低代码平台的想法. 但随着时间的变化,现在市面上低代码各个业务方向的平台都 ...
- docker(一):Develop faster. Run anywhere.
前言 在进行微服务部署时,首先需要进行部署环境的搭建.目前,Docker 已经成为了微服务部署的主流解决方案之一.Docker 可以帮助我们更快地打包.测试以及部署应用程序,从而缩短从编写到部署运行代 ...
- 2022-02-10:k8s安装mongo,yaml如何写?
2022-02-10:k8s安装mongo,yaml如何写? 答案2022-02-10: yaml如下: apiVersion: v1 kind: Service metadata: labels: ...
- 2021-08-02:按公因数计算最大组件大小。给定一个由不同正整数的组成的非空数组 A,考虑下面的图:有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;只有当
2021-08-02:按公因数计算最大组件大小.给定一个由不同正整数的组成的非空数组 A,考虑下面的图:有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记:只有当 ...