当前位置:网站首页>力扣每日一题 06.29 两数相加
力扣每日一题 06.29 两数相加
2022-06-29 17:32:00 【一阵风R】
力扣每日一题 06.29 两数相加
当看到标题写的两数相加是不是感觉很简单,那么好,接下来我们看一下题目:
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例1:

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.
示例2:
输入:l1 = [0], l2 = [0]
输出:[0]
示例3:
输入:l1 = [0], l2 = [0]
输出:[0]
提示:
每个链表中的节点数在范围
[1, 100]内0 <= Node.val <= 9题目数据保证列表表示的数字不含前导零
接下来的我进入了深思。。。
经过了N小时后,有了一丢丢的思路,开始写代码

再经过几小时后,他绿了 ヾ(◍°∇°◍)ノ゙

发现有个100%,当我欣喜若狂的时候,仔细一看,提交次数超过了100%的用户,我干!!!
下面是我的垃圾代码
/**
* 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) {
ListNode header = new ListNode();
ListNode count = header;
int carryNum = 0, type;
while (true) {
type = l1.val + l2.val + carryNum;
carryNum = type / 10;
ListNode tempListNode = new ListNode(type % 10);
count.val = tempListNode.val;
tempListNode = new ListNode();
l1 = l1.next;
l2 = l2.next;
if (l1 != null || l2 != null) {
count.next = new ListNode();
count = count.next;
if (l1 == null) {
l1 = new ListNode(0);
} else if (l2 == null) {
l2 = new ListNode(0);
}
} else if (carryNum > 0 && l1 == null && l2 == null) {
count.next = new ListNode();
count = count.next;
l1 = new ListNode(0);
l2 = new ListNode(0);
}else {
break;
}
}
return header;
}
}
官方代码
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null, tail = null;
int carry = 0;
while (l1 != null || l2 != null) {
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
if (head == null) {
head = tail = new ListNode(sum % 10);
} else {
tail.next = new ListNode(sum % 10);
tail = tail.next;
}
carry = sum / 10;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
if (carry > 0) {
tail.next = new ListNode(carry);
}
return head;
}
}
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/add-two-numbers
边栏推荐
- Browser large screen capture
- 关于KALI使用xshell连接
- Does MySQL support foreign keys
- “授权同意”落地压力大?隐私计算提供一种可能的合规“技术解”
- Openfeign use step polling strategy and weight log4j configuration of openfeign interceptor
- [Oracle] basic knowledge interview questions
- NVIDIA安装最新显卡驱动
- mysql在linux中2003错误如何解决
- mysql游标的作用是什么
- mysql数据库扫盲,你真的知道什么是数据库嘛
猜你喜欢

Online sql to CSV tool

【R语言数据科学】:文本挖掘(以特朗普推文数据为例)

0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram

Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)

Does MySQL support foreign keys

Automatic vending machine

@Difference between component and @configuration
![[R language data science]: Text Mining (taking Trump's tweet data as an example)](/img/4f/09b9885915bee50fb40976a5002730.png)
[R language data science]: Text Mining (taking Trump's tweet data as an example)

A user level thread library based on C language

0 basic self-study STM32 (wildfire) - register lit LED
随机推荐
跨境独立站语言unicode转希伯来语
Mysql高可用集群–MHA
OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
Online sql to CSV tool
What is the MySQL query view command
Help MySQL data analysis with databend
Redis bloom filter and cuckoo filter
在线SQL转CSV工具
正则表达式
2022 software evaluator examination outline
SRM系统可以为企业带来什么价值?
controller、service、dao之间的关系
mysql视图能不能创建索引
First batch! Tencent cloud's ability to pass the solution of the government affairs collaboration platform of the China Academy of ICT
The fixed assets management system enables enterprises to dynamically master assets
R language uses GLM of mass package The Nb function establishes the negative binomial generalized linear model, and the summary function obtains the summary statistical information of the negative bin
LSB hidden items of stream carrier based on assembly implementation
Automatic vending machine
Pancakeswap Technology: development principle of gripper robot system
L'intercepteur handlerinterceptor personnalisé permet l'authentification de l'utilisateur