当前位置:网站首页>02. Add two numbers
02. Add two numbers
2022-07-25 17:09:00 【User 5573316】
#02. Addition of two numbers
difficulty : secondary
Two are given. Non empty The list of is used to represent two non negative integers . among , Their respective digits are based on The reverse Stored in , And each of their nodes can only store a Numbers .
If , Let's add the two numbers together , A new list will be returned to represent their sum .
You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
Example :
Input :(2 -> 4 -> 3) + (5 -> 6 -> 4)
Output :7 -> 0 -> 8
reason :342 + 465 = 807
# Violence law
# Ideas
Cyclic cracking
First, define a root node 0
Then define a variable cur You can modify the memory point in the loop
Then define a temp Used to store carry data , Initialize to 0, If there is no carry, reset to 0
The cycle judgment is not empty val, Empty words are 0
Finally, the child nodes of the root node are returned
# Code
/**
* 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;
}
# Recursive method
# Ideas
Judge first in recursive method l1 Is it empty and l2 Whether it is empty and whether the carry value is 0
Then judge when calling recursion l1 Is it empty ,l2 Is it empty
# Code
/**
* 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;
}
}
边栏推荐
- pgsql有没有好用的图形化管理工具?
- 激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就
- 【obs】转载:OBS直播严重延迟和卡顿怎么办?
- [mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart
- Chapter VI succession
- 【目标检测】TPH-YOLOv5:基于transformer的改进yolov5的无人机目标检测
- HCIP笔记十一天
- Frustrated Internet people desperately knock on the door of Web3
- Outlook 教程,如何在 Outlook 中搜索日历项?
- Getting started with easyUI
猜你喜欢

How to delete Microsoft Pinyin input method in win10

微信公众号开发之消息的自动回复

Jenkins' file parameters can be used to upload files

What is the monthly salary of 10000 in China? The answer reveals the cruel truth of income

The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...

EasyUI modification and DataGrid dialog form control use

7. Dependency injection
Go language series: where does go come from and where will go?

备考过程中,这些“谣言”千万不要信!

Random talk on generation diffusion model: DDPM = Bayesian + denoising
随机推荐
爬虫框架-crawler
聊聊如何用 Redis 实现分布式锁?
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装
异常处理机制专题1
基于redis6.2.4的redis cluster部署
Why 4everland is the best cloud computing platform for Web 3.0
152. Product maximum subarray
How to deploy applications on IPFs using 4everland cli
我们被一个 kong 的性能 bug 折腾了一个通宵
Headless mode of new selenium4.3 in egde browser
Chapter V: process control
[OBS] frame loss and frame priority before transmission
【目标检测】YOLOv5跑通VOC2007数据集(修复版)
Enterprise live broadcast: witness focused products, praise and embrace ecology
win10如何删除微软拼音输入法
虚拟内存管理
China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot
04.寻找两个正序数组的中位数
【obs】发送前丢帧及帧优先级
GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法