当前位置:网站首页>两个链表相加
两个链表相加
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;
}
}
边栏推荐
- The grep command Shell regular expressions, the three musketeers
- Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
- WeChat side: what is consistent hashing, usage scenarios, and what problems does it solve?
- WeChat payment development process
- The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
- 脱光衣服待着就能减肥,当真有这好事?
- 中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
- win10编译x264库(也有生成好的lib文件)
- Too much volume... Tencent was asked on the side that the memory was full, what would happen?
- 发明时代,「幂集创新」事关你我
猜你喜欢
阿里高工带来的20022最新面试总结太香了
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
proto3-2 syntax
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
Simple understanding of ThreadLocal
【无标题】
智驾科技完成C1轮融资,此前2轮已融4.5亿元
曲鸟全栈UI自动化教学(八):框架代码讲解和进一步优化
Shell正则表达式,三剑客之grep命令
ABP 6.0.0-rc.1的新特性
随机推荐
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
2022牛客多校(六)M. Z-Game on grid
The FFmpeg library is configured and used on win10 (libx264 is not configured)
We really need DApp?Really can't meet our fantasy App?
信息系统项目管理师必背核心考点(六十三)项目组合管理的主要过程&DIPP分析
中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
Nature:猪死亡1小时后,器官再次运转
箭头函数和普通函数的常见区别
Blocking, non-blocking, multiplexing, synchronous, asynchronous, BIO, NIO, AIO all in one pot
Senior told me that the giant MySQL is through SSH connection
FFmpeg库在win10上配置使用(不配置libx264)
electron 应用开发优秀实践
学长告诉我,大厂MySQL都是通过SSH连接的
ARP协议原理
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
WPF implements a MessageBox message prompt box with a mask
读写分离后,性能居然提升100%了呀
FFmpeg compiles and installs on win10 (configure libx264)
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
The core key points of microservice architecture