当前位置:网站首页>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;
}
}
边栏推荐
- 龙芯派2代烧写PMON和重装系统
- Network security HSRP protocol
- 搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
- 蓝桥杯学习2022.7.5(上午)
- js 从一个数组对象中取key 和value组成一个新的对象
- Require, require in PHP_ once、include、include_ Detailed explanation of the efficiency of repeated introduction of once class library
- 我为什么支持 BAT 拆掉「AI 研究院」
- Resttemplate details
- TortoiseSVN使用情形、安装与使用
- [cloud resources] what software is good for cloud resource security management? Why?
猜你喜欢
![[js] basic syntax - for loop](/img/7f/6ddc47c062caa7d39538f88e12b1a0.jpg)
[js] basic syntax - for loop

Idea set method annotation and class annotation

Don't know these four caching modes, dare you say you understand caching?

Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care

面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?

【公开课预告】:视频质量评价基础与实践

The development of speech recognition app with uni app is simple and fast.
![[machine learning notes] several methods of splitting data into training sets and test sets](/img/f6/eca239bb4b1764a1495ccd9a868ec1.jpg)
[machine learning notes] several methods of splitting data into training sets and test sets

Redis6 transaction and locking mechanism

Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution
随机推荐
How to apply the updated fluent 3.0 to applet development
Solution to the prompt of could not close zip file during phpword use
[cloud resources] what software is good for cloud resource security management? Why?
stm32逆向入门
Personal component - message prompt
Routing in laravel framework
我为什么支持 BAT 拆掉「AI 研究院」
Basic characteristics and isolation level of transactions
MySQL if else use case use
ELK 企业级日志分析系统
深拷贝真难
redis6数据类型及操作总结
Jasypt configuration file encryption | quick start | actual combat
Source code analysis of etcd database -- peer RT of inter cluster network layer client
Godson 2nd generation burn PMON and reload system
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
Zhubo Huangyu: these spot gold investment skills are not really bad
Network security HSRP protocol
Prefix, infix, suffix expression "recommended collection"
LeetCode_67(二进制求和)
