当前位置:网站首页>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;
}
}
边栏推荐
- [Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
- MySQL index usage and optimization
- 科学论文和学术论文写作
- Redis缓存面临的缓存穿透问题
- Threading(in thread main)
- Find a Go job in 7 days, Conditional statements to learn in Gopher, loop statements, Part 3
- 基于Multisim的函数信号发生器–方波、三角波、正弦波[通俗易懂]
- MySQL row-level locks (row locks, adjacent key locks, gap locks)
- Usage of exists in sql
- 【虚拟化生态平台】树莓派安装虚拟化平台操作流程
猜你喜欢
随机推荐
Single sign-on principle and implementation
矩形脉冲波形的占空比及脉冲和瞬态特征的测量
Usage of exists in sql
lotus-local-net 2k v1.17.0-rc4
便利贴--46{基于移动端长页中分页加载逻辑封装}
"JUC Concurrent Programming - Advanced" 06 - Immutability of Shared Models (Design of Immutable Classes | Use of Immutable Classes | Flyweight Pattern)
After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again
Insertion and deletion of doubly linked list
Can I find a Go job in 7 days?Learn Go with arrays and pointers
Yarn安装配置(vsftpd安装配置)
学自动化测试哪个培训机构好 试听课程后就选了这个地方学习
502 bad gateway causes and solutions
7 天找个 Go 工作,Gopher 要学的条件语句,循环语句 ,第3篇
mysql 自动添加创建时间、更新时间
学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
WSL2安装.NET 6
Experience innovation and iteration through the development of lucky draw mini-programs
R语言:文本(字符串)处理与正则表达式
蓝牙协议栈开发板 STM32F1 跑蓝牙协议栈 –传统蓝牙搜索演示以及实现原理[通俗易懂]
[Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process


![[ 图 论 ]二分图判定及其匹配(基础+提高)](/img/79/56f750e71f558debe3d99404e296e3.png)


![AtCoder—E - Σ[k=0..10^100]floor(X/10^k](/img/be/82cfab00950c1f28d426e76a792906.png)


