当前位置:网站首页>Force deduction daily question 06.29 add two numbers
Force deduction daily question 06.29 add two numbers
2022-06-29 17:41:00 【A gust of wind R】
Try to make a daily question 06.29 Addition of two numbers
Is it easy to add two numbers in the title , So nice , Next, let's look at the topic :
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 = [0], l2 = [0]
Output :[0]
Tips :
The number of nodes in each list is in the range
[1, 100]Inside0 <= Node.val <= 9The title data guarantees that the number indicated in the list does not contain leading zeros
Then I went into deep thinking ...
After N Hours later, , With a lost lost thought , Start writing code

After a few hours , He's green ヾ(◍°∇°◍)ノ゙

Find a 100%, When I am ecstatic , Take a closer look. , Submit More than 100% Users of , I do !!!
Here is my junk code
/**
* 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 header = new ListNode();
ListNode count = header;
int carryNum = 0, type;
while (true) {
type = l1.val + l2.val + carryNum;
carryNum = type / 10;
ListNode tempListNode = new ListNode(type % 10);
count.val = tempListNode.val;
tempListNode = new ListNode();
l1 = l1.next;
l2 = l2.next;
if (l1 != null || l2 != null) {
count.next = new ListNode();
count = count.next;
if (l1 == null) {
l1 = new ListNode(0);
} else if (l2 == null) {
l2 = new ListNode(0);
}
} else if (carryNum > 0 && l1 == null && l2 == null) {
count.next = new ListNode();
count = count.next;
l1 = new ListNode(0);
l2 = new ListNode(0);
}else {
break;
}
}
return header;
}
}
Official code
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null, tail = null;
int carry = 0;
while (l1 != null || l2 != null) {
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
if (head == null) {
head = tail = new ListNode(sum % 10);
} else {
tail.next = new ListNode(sum % 10);
tail = tail.next;
}
carry = sum / 10;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
if (carry > 0) {
tail.next = new ListNode(carry);
}
return head;
}
}
source : Power button (LeetCode) link :https://leetcode.cn/problems/add-two-numbers
边栏推荐
- The aggregate function in the epidisplay package of R language divides numerical variables into different subsets based on factor variables, and calculates the summary statistics and aggregate data. W
- 基于gis三维可视化的智慧城市行业运用
- R语言使用自定义函数编写深度学习线性激活函数、并可视化线性激活函数
- mysql. What is the concept of sock
- Leetcode daily question - 535 Encryption and decryption of tinyurl
- phpunit骚操作之静态类的部分mock
- [try to hack] cookies and sessions
- selenium 组合键操作
- Uploading files using AutoIT
- PCB frame drawing - ad19
猜你喜欢

0 basic self-study STM32 (wildfire) - register lit LED

Online sql to CSV tool

Walk with love, educate and run poor families, and promote public welfare undertakings

Basic operations such as MySQL startup under Windows platform

Opencv+YOLO-V3实现目标跟踪

SRM供应商协同管理系统功能介绍

LeetCode 每日一题——535. TinyURL 的加密与解密

PCB frame drawing - ad19

mysql.sock的概念是什么

DevCloud加持下的青软,让教育“智”上云端
随机推荐
力扣今日题-535. TinyURL 的加密与解密
自动收售报机
What are the usage scenarios for locks in MySQL
Visio标注、批注位置
selenium 组合键操作
两种Controller层接口鉴权方式
Automatic vending machine
Redux源码分析之createStore
位图的详细介绍及模拟实现
剑桥大学教授:经常吃早餐害处多,很危险 - 知乎
剖析下零拷贝机制的实现原理,适用场景和代码实现
填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]
R语言使用自定义函数编写深度学习线性激活函数、并可视化线性激活函数
Maidong Internet won the bid of Dajia Insurance Group
Cross border independent station language Unicode to Hebrew
人脸识别4-百度商用方案调研
最受欢迎的30款开源软件
分割回文串[dp + dfs组合]
Can MySQL views create indexes
How MySQL queries character set codes of tables