当前位置:网站首页>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;
}
}
边栏推荐
- Random talk on generation diffusion model: DDPM = Bayesian + denoising
- [OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?
- Technical difficulties and applications of large humanoid robots
- EasyUI drop-down box, add and put on and off shelves of products
- Chain game development ready-made version chain game system development detailed principle chain game source code delivery
- 7. Dependency injection
- Rainbond插件扩展:基于Mysql-Exporter监控Mysql
- [redis] redis installation
- Rebudget汇报PPT
- [xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>
猜你喜欢

【obs】转载:OBS直播严重延迟和卡顿怎么办?

2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part3):基于规则的问题分类

Jenkins' role based authorization strategy installation configuration

第六章 继承

多租户软件开发架构

【目标检测】TPH-YOLOv5:基于transformer的改进yolov5的无人机目标检测

The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
![[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?](/img/fd/54fcae2bc3f4313e9401c735ef74b8.png)
[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?

EasyUI drop-down box, add and put on and off shelves of products
随机推荐
Headless mode of new selenium4.3 in egde browser
Chapter 4: operators
第五章:流程控制
WPF 实现用户头像选择器
搜狗批量推送软件-搜狗批量推送工具【2022最新】
Jenkins' file parameters can be used to upload files
【obs】转载:OBS直播严重延迟和卡顿怎么办?
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
月薪1万在中国是什么水平?答案揭露残酷的收入真相
【redis】redis安装
在华为昇腾Ascend910上复现swin_transformer
Talk about how to use redis to realize distributed locks?
Hcip notes 11 days
枚举类和魔术值
Multi tenant software development architecture
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
聊聊如何用 Redis 实现分布式锁?
Is it safe to open a securities account in Huatai VIP account
HCIP笔记十一天
第三章、数据类型和变量