当前位置:网站首页>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;
}
}
边栏推荐
- 多线程学习笔记-2.final关键字和不变性
- [ 图 论 ]二分图判定及其匹配(基础+提高)
- The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
- Implement the popup component
- lotus-local-net 2k v1.17.0-rc4
- oracle优化:instr做join条件很慢「建议收藏」
- 淀粉与纤维素
- AWS亚马逊云账号注册,免费申请12个月亚马逊云服务器详细教程
- 一、excel转pdf格式jacob.jar
- The most complete phpmyadmin vulnerability summary
猜你喜欢

In PLC communication error or timeout or download the prompt solution of the model

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

The most complete phpmyadmin vulnerability summary

5 个开源的 Rust Web 开发框架,你选择哪个?

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

The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.

R语言做面板panelvar例子

面试、工作中常用sql大全(建议收藏备用)

众多mock工具,这一次我选对了

分布式id解决方案
随机推荐
In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
第十二章 使用中的 OpenAPI 属性
学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
5 open source Rust web development frameworks, which one do you choose?
Initial JDBC programming
7 days to learn Go, Go structure + Go range to learn
Is the working process of the belt you know the story - actionreducerstore
Obsidian设置图床
Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
下课看着文档走回实验室,我重新拾起了遗忘的SQL运算符
PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用
2022/7/30
Find a Go job in 7 days, Conditional statements to learn in Gopher, loop statements, Part 3
mysql 索引使用与优化
Three-tier architecture service, dao, controller layer
AtCoder—E - Σ[k=0..10^100]floor(X/10^k
准确率(Accuracy)、精度(Precision)、召回率(Recall)和 mAP 的图解
How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
KVM virtualization job
ApiPost 真香真强大,是时候丢掉 Postman、Swagger 了