当前位置:网站首页>02.两数相加
02.两数相加
2022-07-25 17:07:00 【用户5573316】
#02.两数相加
难度:中等
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
# 暴力法
# 思路
循环破解
首先定义一个根节点 0
然后定义一个变量cur 可以在循环中修改内存指向
然后定义一个temp 用来存进位数据,初始化为 0,若无进位重置为0
循环判断非空为val,空的话为0
最终返回根节点的子节点
# 代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int temp = 0;
ListNode pre = new ListNode(0);
ListNode listNode = pre;
while (l1 != null || l2 != null) {
int i = l1 == null ? 0 : l1.val;
int j = l2 == null ? 0 : l2.val;
int sum = i + j + temp;
temp = sum / 10;
sum = sum % 10;
listNode.next = new ListNode(sum);
listNode = listNode.next;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
if(temp == 1) {
listNode.next = new ListNode(temp);
}
return pre.next;
}
# 递归法
# 思路
在递归方法中先判断l1是否为空且 l2 是否为空且进位值是否为0
然后在调用递归时判断 l1 是否为空,l2 是否为空
# 代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return fun(l1, l2, 0);
}
ListNode fun(ListNode l1, ListNode l2, int temp) {
if (l1 == null && l2 == null && temp == 0) {
return null;
}
int i = l1 == null ? 0 : l1.val;
int j = l2 == null ? 0 : l2.val;
int sum = i + j + temp;
temp = sum / 10;
sum = sum % 10;
ListNode listNode = new ListNode(sum);
listNode.next = fun(l1 != null ? l1.next : null, l2 != null ? l2.next : null, temp);
return listNode;
}
}
边栏推荐
- ReBudget:通过运行时重新分配预算的方法,在基于市场的多核资源分配中权衡效率与公平性
- IAAs infrastructure cloud cloud network
- 152. Product maximum subarray
- Frustrated Internet people desperately knock on the door of Web3
- 华泰vip账户证券开户安全吗
- Add batch delete
- Outlook tutorial, how to search for calendar items in outlook?
- Multi tenant software development architecture
- 枚举类和魔术值
- win10自带的框选截图快捷键
猜你喜欢

超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!

7.依赖注入

动态规划题目记录

HCIP笔记十一天

After 20 years of agitation, the chip production capacity has started from zero to surpass that of the United States, which is another great achievement made in China

企业直播风起:目睹聚焦产品,微赞拥抱生态

搜狗批量推送软件-搜狗批量推送工具【2022最新】

Technical difficulties and applications of large humanoid robots

一百个用户眼中,就有一百个QQ

Jenkins' role based authorization strategy installation configuration
随机推荐
Chain game development ready-made version chain game system development detailed principle chain game source code delivery
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装
在华为昇腾Ascend910上复现swin_transformer
GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
[OBS] frame loss and frame priority before transmission
Who moved my memory and revealed the secret of 90% reduction in oom crash
Birui data joins Alibaba cloud polardb open source database community
[target detection] yolov5 Runtong voc2007 dataset (repair version)
Rainbow plug-in extension: monitor MySQL based on MySQL exporter
Talk about how to use redis to realize distributed locks?
Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
IAAs infrastructure cloud cloud network
Hcip notes 11 days
Roson的Qt之旅#100 QML四种标准对话框(颜色、字体、文件、提升)
HCIP笔记十一天
Gtx1080ti fiber HDMI interference flash screen 1080ti flash screen solution
What is the monthly salary of 10000 in China? The answer reveals the cruel truth of income
unity 最好用热更方案卧龙 wolong
新版selenium4.3在egde浏览器的无头模式
霸榜COCO!DINO: 让目标检测拥抱Transformer