当前位置:网站首页>两个链表相加
两个链表相加
2022-08-09 12:01:00 【爱敲代码的Harrison】
题目
力扣链接:两数相加

方法一:把链表转换为数字再相加?但是链表的长度可能会很长,就算是long类型长度也不够,所以此方法不可取!
方法二:利用容器?遍历链表,然后生成一个相应长度的数组,再将链表中的每个值拷贝进数组;另外一个链表也是如此。然后将数组中的每一个数对应相加,但是要处理好进位!最后通过数组形式还原成链表。此方法可以在笔试时候用,因为笔试不会太看重空间复杂度,并且此方法coding相比第三种方法稍微没那么容易出错,很可能笔试时候想不到第三种方法。
方法三:看代码
代码
package com.harrison.class06;
/** * @author Harrison * @create 2022-06-19-16:22 * @motto 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。 */
public class Code08_AddTwoNumbers {
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;
}
}
public ListNode addTwoNumbers(ListNode head1, ListNode head2) {
int len1 = listLen(head1);
int len2 = listLen(head2);
ListNode l = len1 >= len2 ? head1 : head2;
ListNode s = l == head1 ? head2 : head1;
ListNode curL = l;
ListNode curS = s;
ListNode last = curL;
int carry = 0;
int curNum = 0;
while (curS != null) {
curNum = curS.val + curL.val + carry;
curL.val = (curNum % 10);
carry=(curNum/10);
last=curL;
curL=curL.next;
curS= curS.next;
}
while(curL!=null){
curNum=curL.val+carry;
curL.val=(curNum%10);
carry=(curNum/10);
last=curL;
curL= curL.next;
}
if(carry!=0){
last.next=new ListNode(1);
}
return l;
}
public static int listLen(ListNode head) {
int len = 0;
while (head != null) {
len++;
head = head.next;
}
return len;
}
}
边栏推荐
- 国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
- We really need DApp?Really can't meet our fantasy App?
- 【无标题】
- 脱光衣服待着就能减肥,当真有这好事?
- OpenSSF的开源软件风险评估工具:Scorecards
- 无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
- The core key points of microservice architecture
- 放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
- Adalvo收购其首个品牌产品Onsolis
- 阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
猜你喜欢

西湖大学教授怎么看AI制药革命?|量子位智库圆桌实录

HAproxy:负载均衡

MySQL 原理与优化,Group By 优化 技巧

Scala Advanced (7): Collection Content Summary (Part 1)

h264 protocol

Manchester city launch emotional intelligence scarf can be detected, give the fans

proto3-2 syntax

00后写个暑假作业,被监控成这笔样

900页数学论文证明旋转的黑洞不会爆炸,丘成桐:30多年来广义相对论首次重大突破...

读写分离后,性能居然提升100%了呀
随机推荐
goalng-sync/atomic原子操作
【小程序】低代码+小游戏=小游戏可视化开发
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
#Internet of Things essay#Xiaoxiong pie equipment development actual combat
Double pointer - the role of char **, int **
微服务架构的核心关键点
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
MySQL principle and optimization of Group By optimization techniques
【微服务~远程调用】整合RestTemplate、WebClient、Feign
The grep command Shell regular expressions, the three musketeers
ACM01 Backpack problem
GET请求和POST请求区别
LeetCode #101. 对称二叉树
信息系统项目管理师必背核心考点(六十三)项目组合管理的主要过程&DIPP分析
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
【Untitled】
标准C语言学习总结14
学长告诉我,大厂MySQL都是通过SSH连接的
Adalvo收购其首个品牌产品Onsolis
8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository