当前位置:网站首页>Leetcode top 100 question 2 Add two numbers
Leetcode top 100 question 2 Add two numbers
2022-07-01 05:25:00 【weixin_ forty-one million two hundred and sixty-two thousand on】


public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int[] num1=ListToArray(l1);
int[] num2=ListToArray(l2);
int i1=arrayToInt(num1);
int i2=arrayToInt(num2);
int result=i1+i2;
int[] numResult=IntToArray(result);
return headAdd(numResult);
}
public int[] ListToArray(ListNode l){
ArrayList<Integer> num=new ArrayList<Integer>();
int length=0;
while(l!=null){
num.add(l.val);
l=l.next;
length++;
}
int[] result=new int[length];
for(int i=length-1,j=0;i>=0;i--,j++){
result[i]=num.get(j);
}
return result;
}
// Returns a reverse order int,[1,2,3]=321
public int arrayToInt(int[] num){
int result=0;
for(int i=num.length-1,l=num.length;i>=0;i--,l--){
result+=num[i]*Math.pow(10,l-1);
}
return result;
}
public int[] IntToArray(int value){
String str=String.valueOf(value);
int[] intArray = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
// Traverse str Add each digit as intArray
Character ch = str.charAt(i);
intArray[i] = Integer.parseInt(ch.toString());
}
return intArray;
}
public ListNode headAdd(int[] num){
ListNode head=new ListNode(num[num.length-1],null);
ListNode l=head;
for(int i=num.length-2;i>=0;i--){
l.next=new ListNode(num[i],null);
l=l.next;
}
return head;
}
}
The train of thought is
1, Apply for an array of linked list lengths , Assign the value of the linked list to the array .
2, Convert these two arrays to int
3, Add two numbers
4, Convert the result to an array
5, Traversal through the array from tail to head , Use the tail interpolation method to build the linked list
边栏推荐
- Global and Chinese market of 3D design and modeling software 2022-2028: Research Report on technology, participants, trends, market size and share
- QT waiting box production
- Unity drags and modifies scene camera parameters under the editor
- Use and principle of wait notify
- [Yugong series] February 2022 Net architecture class 005 ABP vNext Net core web application getting started configuration
- Programmers dig "holes" to get rich: if they find a loophole, they will be rewarded 12.72 million yuan
- CockroachDB: The Resilient Geo-Distributed SQL Database 论文阅读笔记
- Causes of short circuit of conductive slip ring and Countermeasures
- CentOS 7使用yum安装PHP7.0
- Global and Chinese markets for business weather forecasting 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Copy baby prompt: material cannot be empty. How to solve it?
![[RootersCTF2019]babyWeb](/img/b4/aa8f8e107a9dacbace72d4717b1834.png)
[RootersCTF2019]babyWeb

Series of improving enterprise product delivery efficiency (1) -- one click installation and upgrade of enterprise applications

Summary of spanner's paper

Principle, technology and implementation scheme of data consistency in distributed database

Practice of combining rook CEPH and rainbow, a cloud native storage solution

导电滑环短路的原因以及应对措施

CockroachDB: The Resilient Geo-Distributed SQL Database 论文阅读笔记

工业导电滑环的应用

Leetcode316- remove duplicate letters - stack - greedy - string
随机推荐
实战:redux的基本使用
Using nocalhost to develop microservice application on rainbow
CockroachDB: The Resilient Geo-Distributed SQL Database 论文阅读笔记
担心侵权?必备无版权素材网站分享,不用担心视频剪辑缺素材
Application of industrial conductive slip ring
Use and principle of AQS related implementation classes
导电滑环短路的原因以及应对措施
导电滑环使用的注意事项
Rainbow combines neuvector to practice container safety management
Global and Chinese market of metal oxide semiconductor field effect transistors 2022-2028: Research Report on technology, participants, trends, market size and share
基于TI DRV8424驱动步进电机实现调速和行程控制
Explanation of characteristics of hydraulic slip ring
Programmers dig "holes" to get rich: if they find a loophole, they will be rewarded 12.72 million yuan
Set set detailed explanation
了解 JVM 中几个相关问题 — JVM 内存布局、类加载机制、垃圾回收
QT等待框制作
Numeric amount plus comma; JS two methods of adding three digits and a comma to numbers; JS data formatting
Series of improving enterprise product delivery efficiency (1) -- one click installation and upgrade of enterprise applications
Dynamic verification of new form items in El form; El form verifies that the dynamic form V-IF does not take effect;
0xc000007b the application cannot start the solution normally (the pro test is valid)