当前位置:网站首页>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;
}
}
边栏推荐
- pycharm汉化教程(碧蓝幻想汉化插件安装)
- Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
- Usage of exists in sql
- 分布式事务Seata详细使用教程
- Find a Go job in 7 days, Conditional statements to learn in Gopher, loop statements, Part 3
- St. Regis Takeaway Project: New dishes and dishes paged query
- 使用内存映射加快PyTorch数据集的读取
- 502 bad gateway causes and solutions
- mysql 索引使用与优化
- deeplab实现自己遥感地质分割数据集
猜你喜欢
[Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
Redis学习笔记-3.慢查询和其他高级数据结构
Usage of exists in sql
Redis-基础
IDEA 配置方法注释自动参数
Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
学自动化测试哪个培训机构好 试听课程后就选了这个地方学习
Power BI----几个常用的分析方法和相适应的视觉对象
[Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
随机推荐
实现弹框组件
Threading(in thread main)
SQLServer2019 installation (Windows)
WSL2安装.NET 6
IDEA 配置方法注释自动参数
cesium-Web网页优化进阶
oracle优化:instr做join条件很慢「建议收藏」
unity computeshader的可读写buffer
淀粉与纤维素
Docker installs canal and mysql for simple testing and achieves cache consistency between redis and mysql
拥抱趋势!阿里这套微服务开源框架权威手册,实战到底层细致清晰
学习笔记 Golang 写入文件(io.WriteString、ioutil.WriteFile、file.Write、write.WriteString)
Single sign-on principle and implementation
IBM SPSS Statistics 28软件安装包下载及安装教程
MySQL limit paging query and performance issues
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
《MySQL高级篇》五、InnoDB数据存储结构
The latest MySql installation teaching, very detailed
1161. 最大层内元素和 (二叉树的层序遍历)
3.网页信息解析方法:Xpath与BeautifulSoup