当前位置:网站首页>LeetCode_ 2 (add two numbers)
LeetCode_ 2 (add two numbers)
2022-07-05 14:13:00 【***】
Title Description :
Here are two for you Non empty The linked list of , Represents two nonnegative integers . Each of them is based on The reverse Stored in , And each node can only store a Numbers .
Please add up the two numbers , And returns a linked list representing sum in the same form .
You can assume that in addition to the numbers 0 outside , Neither of these numbers 0 start .
Example 1:
Input :l1 = [2,4,3], l2 = [5,6,4]
Output :[7,0,8]
explain :342 + 465 = 807.
Example 2:
Input :l1 = [0], l2 = [0]
Output :[0]
Example 3:
Input :l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output :[8,9,9,9,0,0,0,1]
Tips :
The number of nodes in each list is in the range [1, 100] Inside
0 <= Node.val <= 9
The title data guarantees that the number indicated in the list does not contain leading zeros
/**
* 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; // Create head nodes and pointer nodes
int flag=0; // Carry mark
while(l1!=null||l2!=null){
// Traverse two single linked lists
int n1=l1!=null?l1.val:0; // Get the value of the current node of the first linked list , If the current node is empty , Then fill in zero
int n2=l2!=null?l2.val:0; // ditto
int cur=n1+n2+flag; // The value that should be filled in the linked list of calculation results
if(head==null){
// If it is the first value , Then create a node and connect the head pointer
l=new ListNode(cur%10);
head=l;
}else{
// Create a new node, fill in the result and move the pointer back
l.next=new ListNode(cur%10);
l=l.next;
}
flag=cur/10; // Judge whether carry is needed
if(l1!=null)l1=l1.next; // Judge before moving the pointer back , To avoid pointer exceptions
if(l2!=null)l2=l2.next;
}
if(flag>0)l.next=new ListNode(flag);
return head;
}
}
边栏推荐
- 最长公共子序列 - 动态规划
- openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
- poi设置列的数据格式(有效)
- R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
- R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
- tidb-dm报警DM_sync_process_exists_with_error排查
- [js] basic syntax - for loop
- 别不服气。手机功能升级就是强
- 治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东
- The simplest way to open more functions without certificates
猜你喜欢

分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少

Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder

Scenario based technology architecture process based on tidb - Theory

OSI and tcp/ip protocol cluster

区间 - 左闭右开

Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module

Sorter evolution of ticdc 6.0 principle

Introduction, installation, introduction and detailed introduction to postman!

Tdengine biweekly selection of community issues | phase III

Redis如何实现多可用区?
随机推荐
VC development of non MFC program memory leak tracking code
R language dplyr package select function, group_ By function, mutate function and cumsum function calculate the cumulative value of the specified numerical variable in the dataframe grouping data and
最简单不用证书也可以多开功能的方式
Implementation process of WSDL and soap calls under PHP5
PHP5下WSDL,SOAP调用实现过程
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
Use the word "new" to attract curious people
Anchor navigation demo
Simple process of penetration test
微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法
最长公共子序列 - 动态规划
mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
Blue Bridge Cup study 2022.7.5 (morning)
Show strength. In this way, the mobile phone will not be difficult to move forward
如何将 DevSecOps 引入企业?
LeetCode_69(x 的平方根 )
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
矩阵链乘 - 动态规划实例
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
upload (1-6)
