当前位置:网站首页>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;
}
}
边栏推荐
- 学习笔记 Golang 写入文件(io.WriteString、ioutil.WriteFile、file.Write、write.WriteString)
- CoCube群机器人预览→资讯剧透←
- Docker搭建Mysql主从复制
- 502 bad gateway causes and solutions
- 分布式事务Seata详细使用教程
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- Sql optimization summary!detailed!(Required for the latest interview in 2021)
- 掌握SSR
- web安全入门-黑苹果MAC系统安装
- 新人学习小熊派华为iot介绍
猜你喜欢
随机推荐
7 days to learn Go, Go structure + Go range to learn
PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用
KVM virtualization job
redis-enterprise use
mysql 索引使用与优化
准确率(Accuracy)、精度(Precision)、召回率(Recall)和 mAP 的图解
Usage of exists in sql
面试、工作中常用sql大全(建议收藏备用)
《MySQL高级篇》五、InnoDB数据存储结构
SQL study notes - REGEXP operator
Distributed id solution
[Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
web安全入门-黑苹果MAC系统安装
初始JDBC 编程
[Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
「R」使用ggpolar绘制生存关联网络图
2022/7/30
3.网页信息解析方法:Xpath与BeautifulSoup
学自动化测试哪个培训机构好 试听课程后就选了这个地方学习
AWS Amazon cloud account registration, free application for 12 months Amazon cloud server detailed tutorial









