当前位置:网站首页>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;
}
}
边栏推荐
- 昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
- 为什么我认识的机械工程师都抱怨工资低?
- 展现强大。这样手机就不会难前进
- 3W principle [easy to understand]
- 04_ Use of solrj7.3 of solr7.3
- [buuctf.reverse] 152-154
- 2022 machine fitter (Advanced) test question simulation test question bank simulation test platform operation
- What is the ranking of GF futures? Is it safe and reliable to open an account for GF futures online?
- Current situation, trend and view of neural network Internet of things in the future
- 如何深入理解“有限状态机”的设计思想?
猜你喜欢

In addition to the root directory, other routes of laravel + xampp are 404 solutions

How to introduce devsecops into enterprises?

神经网络物联网未来现状和趋势及看法

Anchor navigation demo

魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问

What is the future development trend of neural network Internet of things

Financial one account Hong Kong listed: market value of 6.3 billion HK $Ye wangchun said to be Keeping true and true, long - term work

Current situation, trend and view of neural network Internet of things in the future

Xampp configuring multiple items

让秒杀狂欢更从容:大促背后的数据库(下篇)
随机推荐
别不服气。手机功能升级就是强
Hongmeng fourth training
循环不变式
The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
Discussion on memset assignment
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
基于 TiDB 场景式技术架构过程 - 理论篇
2022 machine fitter (Advanced) test question simulation test question bank simulation test platform operation
POI set the data format of the column (valid)
[js] basic syntax - for loop
R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
广发期货排名多少?网上办理广发期货开户安全可靠吗?
OSI and tcp/ip protocol cluster
matlab学习2022.7.4
牛客网:拦截导弹
基于伯努利原理的速度监测芯片可用于天然气管道泄露检测
poi设置列的数据格式(有效)
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
