当前位置:网站首页>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;
}
}
边栏推荐
- Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
- What is the future development trend of neural network Internet of things
- Assembly language
- Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- Fault analysis | analysis of an example of MySQL running out of host memory
- 一网打尽异步神器CompletableFuture
- Matlab learning 2022.7.4
- 04_solr7.3之solrJ7.3的使用
- Don't be unconvinced. Mobile phone function upgrade is strong
猜你喜欢
LeetCode_2(两数相加)
Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder
让秒杀狂欢更从容:大促背后的数据库(下篇)
IP packet header analysis and static routing
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
TiCDC 6.0原理之Sorter演进
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
金融壹賬通香港上市:市值63億港元 葉望春稱守正篤實,久久為功
Deep copy is hard
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
随机推荐
非技术部门,如何参与 DevOps?
瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿
WebRTC的学习(二)
Liste des liens (simple)
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
清大科越冲刺科创板:年营收2亿 拟募资7.5亿
3W principle [easy to understand]
Kunlun Taike rushes to the scientific innovation board: the annual revenue is 130million, and it plans to raise 500million. CETC Taiji holds 40% of the shares
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
Blue Bridge Cup study 2022.7.5 (morning)
金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功
The simplest way to open more functions without certificates
Assembly language
C - Divisors of the Divisors of An Integer Gym - 102040C
R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
怎么叫一手一机的功能方式
如何将 DevSecOps 引入企业?
Postman简介、安装、入门使用方法详细攻略!
01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
TiFlash 面向编译器的自动向量化加速