当前位置:网站首页>LeetCode - 025. 链表中的两数相加
LeetCode - 025. 链表中的两数相加
2022-07-31 11:19:00 【NPE~】
LeetCode - 025. 链表中的两数相加

方法一:通过链表反转 + 分情况
首先,我们将链表分为一长一短,由此链表相加可以分为三种情况:
我们用curS表示当前短链表走到的位置,curL表示当前长链表走到的位置,carray表示进位,last用于存储null的上一个节点,方便当长短链表都走完而carry不为0时,新增节点
①长链表没走完,短链表也没走完
//有长有短
while(curS != null){
int sum = curL.val + curS.val + carray;
curL.val = sum % 10;
carray = sum / 10;
last = curL;//存储null的上一个节点
curL = curL.next;
curS = curS.next;
}
②只有长
//有长无短
while(curL != null){
int sum = curL.val + carray;
curL.val = sum % 10;
carray = sum / 10;
last = curL;
curL = curL.next;
}
③无长无短
//无长无短
if(carray != 0){
last.next = new ListNode(1);
}
④反转链表(注意最后结果也需要反转)
public ListNode reverse(ListNode head){
if(head == null){
return null;
}
ListNode pre = null;
ListNode cur = head;
ListNode next = cur.next;
while(cur != null){
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
全部代码:
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//获取两链表长度
int len1 = getLength(l1);
int len2 = getLength(l2);
l1 = reverse(l1);
l2 = reverse(l2);
ListNode l = len1 >= len2 ? l1 : l2;//长链表
ListNode s = len1 < len2 ? l1 : l2;
ListNode curL = l;
ListNode curS = s;
ListNode last = curL;
int carray = 0;//进位
int sum = 0;
//有长有短
while(curS != null){
sum = curL.val + curS.val + carray;
curL.val = sum % 10;
carray = sum / 10;
last = curL;//存储null的上一个节点
curL = curL.next;
curS = curS.next;
}
//有长无短
while(curL != null){
sum = curL.val + carray;
curL.val = sum % 10;
carray = sum / 10;
last = curL;
curL = curL.next;
}
//无长无短
if(carray != 0){
last.next = new ListNode(1);
}
return reverse(l);
}
public int getLength(ListNode l){
int res = 0;
if(l == null) return res;
while(l != null){
res++;
l = l.next;
}
return res;
}
public ListNode reverse(ListNode head){
if(head == null){
return null;
}
ListNode pre = null;
ListNode cur = head;
ListNode next = cur.next;
while(cur != null){
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
方法二:通过Deque实现【使用栈的特性实现】
首先,将两个不同的链表分别入不同的栈,再利用栈的特性分别出栈
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Deque<Integer> stack1 = new ArrayDeque<>();
Deque<Integer> stack2 = new ArrayDeque<>();
while(l1 != null){
stack1.push(l1.val);
l1 = l1.next;
}
while(l2 != null){
stack2.push(l2.val);
l2 = l2.next;
}
int carray = 0;//进位
ListNode res = null;
while(!stack1.isEmpty() || !stack2.isEmpty() || carray != 0){
int a = stack1.isEmpty() ? 0 : stack1.pop();
int b = stack2.isEmpty() ? 0 : stack2.pop();
int sum = a + b + carray;
carray = sum / 10;
int val = sum % 10;
ListNode cur = new ListNode(val);
cur.next = res;
res = cur;
}
return res;
}
}
边栏推荐
- 第十二章 使用中的 OpenAPI 属性
- Three-tier architecture service, dao, controller layer
- 7 天能找到 Go 工作吗?学学 Go 数组和指针试试
- 502 bad gateway原因、解决方法
- The latest MySql installation teaching, very detailed
- keras自带数据集(横线生成器)
- [Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process
- SQLServer2019 installation (Windows)
- cesium-Web网页优化进阶
- "JUC Concurrent Programming - Advanced" 06 - Immutability of Shared Models (Design of Immutable Classes | Use of Immutable Classes | Flyweight Pattern)
猜你喜欢

安装MYSQL遇到问题:write configuration file卡主

2022/7/28

St. Regis Takeaway Project: New dishes and dishes paged query

Single sign-on principle and implementation

apisix-入门使用篇

若枚举映射的值不存在,则不进行反序列化

IBM SPSS Statistics 28软件安装包下载及安装教程

AWS亚马逊云账号注册,免费申请12个月亚马逊云服务器详细教程

Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)

AWS Amazon cloud account registration, free application for 12 months Amazon cloud server detailed tutorial
随机推荐
Initial JDBC programming
[Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
Android studio连接MySQL并完成简单的登录注册功能
unity-shader-2
SQLServer2019 installation (Windows)
Redis - Basics
The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
Android studio connects to MySQL and completes simple login and registration functions
【Web技术】1397- 深入浅出富文本编辑器
Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
台达PLC出现通信错误或通信超时或下载时提示机种不符的解决办法总结
掌握SSR
Master SSR
Single sign-on principle and implementation
Detailed tutorial on distributed transaction Seata
IBM SPSS Statistics 28软件安装包下载及安装教程
deeplab实现自己遥感地质分割数据集
SQLSERVER merges subquery data into one field
deeplab implements its own remote sensing geological segmentation dataset
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了