当前位置:网站首页>34. Add two numbers
34. Add two numbers
2022-07-24 12:47:00 【Little happy】
2. Addition of two numbers
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]
Just go through it
1、 At the same time, enumerate two linked lists from scratch , take l1 and l2 The element pointed to by the pointer is saved to t in , then t % 10 The element of is saved to dummy In the list , Again t / 10 Remove the stored elements ,l1 and l2 At the same time, move back one space
2、 When all elements are traversed , If t != 0, And then t Deposit to dummy In the list
/** * 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 dummy = new ListNode(0);
ListNode p = dummy;
int t = 0;
while(l1!=null||l2!=null){
if(l1!=null) {
t = t+l1.val;l1=l1.next;}
if(l2!=null) {
t = t +l2.val;l2 = l2.next;}
ListNode a = new ListNode(t%10);
p.next = a;
p = a;
t = t/10;
}
if(t!=0){
ListNode a = new ListNode(t%10);
p.next = a;
p = a;
}
return dummy.next;
}
}
边栏推荐
- What kind of experience is a monthly salary of 30000 yuan? Can we achieve this level as we media
- 使用Jenkins搭建CI服务器
- Use abp Zero builds a third-party login module (III): web side development
- ASP. Net core deployment Manual: 1. Deployment Basics
- 突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战
- 自己实现is_default_constructible
- Support liuhaiping
- 元宇宙更多的功能和作用在于对于传统生活方式和生产方式的深度改造
- thinkphp 实现数据库备份
- Use abp Zero builds a third-party login module (4): wechat applet development
猜你喜欢

Analysis of ISP one click download principle in stm32

Support liuhaiping

Basic SQL server operation problems - only when lists are used and identity_ Only when insert is on can the display value be set for the identification column in the table
![[C language] detailed knowledge of document operation](/img/2a/f2976d80212d9a38ea0457916a1db9.png)
[C language] detailed knowledge of document operation

setAttribute、getAttribute、removeAttribute

突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战

thinkphp 实现数据库备份

SSM在线考试系统含文档

Wechat applet generates QR code

Wechat applet - drawing dashboard
随机推荐
SSM online rental and sales platform multi city version
Okaleido tiger NFT is about to log in to binance NFT platform
Native Crash的一切
Summary of recent interviews
SSH服务突然连接不了案例总结
我在一个模块工程中使用注解配置了redis的序列化, 然后在另外一个模块引入这个模块,为什么这个配置
setAttribute、getAttribute、removeAttribute
权限系统就该这么设计,yyds
Video realizes the control of video progress, playback and pause
Set up CI server with Jenkins
23. Spiral matrix
for mysql
3. Realize snake and basic game interface
Buckle exercise - 35 combination sum II
QWaitCondition 的正确使用方法
猿人学第六题
Reserved instances & Savings Plans
Why does 2.tostring() report an error
STM32 - Fundamentals of C language
Implementing deep learning framework from zero -- further exploration of the implementation of multilayer bidirectional RNN