当前位置:网站首页>LeetCode_2(两数相加)
LeetCode_2(两数相加)
2022-07-05 13:51:00 【***】
题目描述:
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 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 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:[8,9,9,9,0,0,0,1]
提示:
每个链表中的节点数在范围 [1, 100] 内
0 <= Node.val <= 9
题目数据保证列表表示的数字不含前导零
/**
* 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 head=null,l=null; //创建头节点和指针节点
int flag=0; //进位标志
while(l1!=null||l2!=null){
//遍历两个单链表
int n1=l1!=null?l1.val:0; //取出第一个链表当前节点的值,若当前节点为空,则补零
int n2=l2!=null?l2.val:0; //同上
int cur=n1+n2+flag; //计算结果链表应当填入的值
if(head==null){
//若为第一个值,则创建节点并连接头指针
l=new ListNode(cur%10);
head=l;
}else{
//创建新节点填入结果并将指针后移
l.next=new ListNode(cur%10);
l=l.next;
}
flag=cur/10; //判断是否需要进位
if(l1!=null)l1=l1.next; //指针后移前判断一下,以免指针异常
if(l2!=null)l2=l2.next;
}
if(flag>0)l.next=new ListNode(flag);
return head;
}
}
边栏推荐
- 荐号 | 有趣的人都在看什么?
- When there are too many input boxes such as input transmitted at one time in the form, the post data is intercepted
- Liar report query collection network PHP source code
- Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
- Redis6 data type and operation summary
- LeetCode_67(二进制求和)
- asp.net 读取txt文件
- Self built shooting range 2022
- ZABBIX monitoring
- aspx 简单的用户登录
猜你喜欢
法国学者:最优传输理论下对抗攻击可解释性探讨
Internal JSON-RPC error. {"code":-32000, "message": "execution reverted"} solve the error
Laravel framework operation error: no application encryption key has been specified
几款分布式数据库的对比
How to deal with the Yellow Icon during the installation of wampserver
uplad_ Labs first three levels
What happened to the communication industry in the first half of this year?
嵌入式软件架构设计-消息交互
::ffff:192.168.31.101 是一个什么地址?
Idea remote debugging agent
随机推荐
NFT value and white paper acquisition
web3.eth. Filter related
TortoiseSVN使用情形、安装与使用
ELFK部署
Zhubo Huangyu: these spot gold investment skills are not really bad
嵌入式软件架构设计-消息交互
[public class preview]: basis and practice of video quality evaluation
Scientific running robot pancakeswap clip robot latest detailed tutorial
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
[cloud resources] what software is good for cloud resource security management? Why?
leetcode 10. Regular expression matching regular expression matching (difficult)
Apicloud studio3 WiFi real machine synchronization and WiFi real machine preview instructions
深拷贝真难
【公开课预告】:视频质量评价基础与实践
53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
Prefix, infix, suffix expression "recommended collection"
Programmer growth Chapter 8: do a good job of testing
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
LeetCode_69(x 的平方根 )