当前位置:网站首页>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;
}
}
边栏推荐
- [xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>
- Technical difficulties and applications of large humanoid robots
- What are the free low code development platforms?
- 【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装
- Roson的Qt之旅#99 QML表格控件-TableView
- Chapter V: process control
- 生成扩散模型漫谈:DDPM = 贝叶斯 + 去噪
- Test framework unittest command line operation and assertion method
- Exception handling mechanism topic 1
- 大型仿人机器人的技术难点和应用情况
猜你喜欢

Text translation software - text batch translation converter free of charge

在华为昇腾Ascend910上复现swin_transformer

Rebudget汇报PPT

win10如何删除微软拼音输入法

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part4):结合问题分类的问题解析与检索语句生成

博后招募 | 西湖大学机器智能实验室招聘博士后/助理研究员/科研助理

月薪1万在中国是什么水平?答案揭露残酷的收入真相

简述redis集群的实现原理

Multi tenant software development architecture

Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
随机推荐
Box selection screenshot shortcut key of win10
企业直播风起:目睹聚焦产品,微赞拥抱生态
3D semantic segmentation - scribed supervised lidar semantic segmentation
win10自带的框选截图快捷键
Dynamic planning topic record
unity 最好用热更方案卧龙 wolong
Automatic reply of wechat official account development message
7.依赖注入
Page table cache of Linux kernel source code analysis
Random talk on generation diffusion model: DDPM = Bayesian + denoising
Chapter 4: operators
From digitalization to intelligent operation and maintenance: what are the values and challenges?
Outlook tutorial, how to search for calendar items in outlook?
[MySQL] takes you to the database
2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析
在华为昇腾Ascend910上复现swin_transformer
Use huggingface to quickly load pre training models and datasets in moment pool cloud
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
win10如何删除微软拼音输入法
第六章 继承