当前位置:网站首页>两个链表相加
两个链表相加
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;
}
}
边栏推荐
- 又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
- [Interview high-frequency questions] Linked list high-frequency questions that can be gradually optimized
- Batch大小不一定是2的n次幂!ML资深学者最新结论
- 无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
- ThreadLocal的简单理解
- HAproxy:负载均衡
- GET请求和POST请求区别
- 鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
- h264 protocol
- Web console control edit box
猜你喜欢

听声辨物,这是AI视觉该干的???|ECCV 2022

JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning

C# 获取系统已安装的.NET版本

张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来

Too much volume... Tencent was asked on the side that the memory was full, what would happen?

Intranet penetration tool ngrok usage tutorial

The grep command Shell regular expressions, the three musketeers

罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...

1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint

用皮肤“听”音乐,网友戴上这款装备听音乐会:仿佛住在钢琴里
随机推荐
西湖大学教授怎么看AI制药革命?|量子位智库圆桌实录
AQS同步组件-FutureTask解析和用例
Senior told me that the giant MySQL is through SSH connection
Go-based web access parameters
ThreadLocal的简单理解
微服务架构的核心关键点
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
Nature:猪死亡1小时后,器官再次运转
Shell之常用小工具(sort、uniq、tr、cut)
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
专业人士使用的 11 种渗透测试工具
shell脚本------函数的格式,传参,变量,递归,数组
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
一甲子,正青春,CCF创建六十周年庆典在苏州举行
Scala Advanced (7): Collection Content Summary (Part 1)
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
2022 Niu Ke Duo School (6) M. Z-Game on grid
荣耀携手Blue Yonder,加快企业战略增长
学长告诉我,大厂MySQL都是通过SSH连接的