当前位置:网站首页>力扣每日一题 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
边栏推荐
- Function calculation asynchronous task capability introduction - task trigger de duplication
- PCB frame drawing - ad19
- 力扣今日题-535. TinyURL 的加密与解密
- 正则表达式
- How to create and delete MySQL triggers
- 跨境独立站语言unicode转希伯来语
- Multi mode concurrent implementation of tortoise and rabbit race in go language
- 如何创建虚拟形象
- mysql游标的作用是什么
- High landing pressure of "authorization and consent"? Privacy computing provides a possible compliance "technical solution"
猜你喜欢
随机推荐
SCM系统是什么?供应链管理系统有哪些优势?
Does MySQL support foreign keys
一次采集JSON解析错误的修复
NVIDIA安装最新显卡驱动
mysql视图能不能创建索引
KUKA robot external axis configuration what you must know
Help MySQL data analysis with databend
Freedom自由协议质押挖矿系统开发
Browser large screen capture
Online text digit recognition list summation tool
R语言使用自定义函数编写深度学习Leaky ReLU激活函数、并可视化Leaky ReLU激活函数
力扣今日题-535. TinyURL 的加密与解密
How to solve the 2003 error of MySQL in Linux
sequential detector
What are the advantages of SaaS services
ICML 2022 | transferable imitation learning method based on decoupling gradient optimization
自定义HandlerInterceptor拦截器实现用户鉴权
0基础自学STM32(野火)——使用寄存器点亮LED——GPIO功能框图讲解
windows平台下的mysql启动等基本操作
Leetcode daily question - 535 Encryption and decryption of tinyurl









