当前位置:网站首页>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;
}
}
边栏推荐
- Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
- 中国芯片自给率大幅提升,导致外国芯片库存高企而损失惨重,美国芯片可谓捧起石头砸自己的脚...
- [redis] redis installation
- [target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
- 基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
- jenkins的Role-based Authorization Strategy安装配置
- Sogou batch push software - Sogou batch push tool [2022 latest]
- HCIP笔记十一天
- Hcip notes 12 days
- Jenkins' role based authorization strategy installation configuration
猜你喜欢

Multi tenant software development architecture

Briefly describe the implementation principle of redis cluster

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

How to install govendor and open a project

WPF 实现用户头像选择器

3D semantic segmentation - scribed supervised lidar semantic segmentation

Outlook tutorial, how to search for calendar items in outlook?

【目标检测】YOLOv5跑通VisDrone数据集

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

HCIP笔记十二天
随机推荐
7.依赖注入
用秩讨论线性方程组的解/三个平面的位置关系
[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart
Chapter VI succession
stm32F407------SPI
[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?
霸榜COCO!DINO: 让目标检测拥抱Transformer
Chapter III data types and variables
What are the free low code development platforms?
Technical difficulties and applications of large humanoid robots
【目标检测】YOLOv5跑通VOC2007数据集(修复版)
第五章:流程控制
Enumeration classes and magic values
大型仿人机器人的技术难点和应用情况
03.无重复字符的最长子串
中国芯片自给率大幅提升,导致外国芯片库存高企而损失惨重,美国芯片可谓捧起石头砸自己的脚...
152. Product maximum subarray
Getting started with easyUI
【目标检测】YOLOv5跑通VisDrone数据集
From digitalization to intelligent operation and maintenance: what are the values and challenges?