当前位置:网站首页>BM11 链表相加(二)
BM11 链表相加(二)
2022-07-28 17:13:00 【爱生活爱编程a】

思路:先反转两个链表然后依次进行值相加并建立新节点连接到结果链表中,最后反转结果链表即可.
解法1
public class Solution {
/** * * @param head1 ListNode类 * @param head2 ListNode类 * @return ListNode类 */
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
if(head1 == null && head2 == null){
return null;
}
if(head1 == null){
return head2;
}
if(head2 == null){
return head1;
}
// 1: 反转两个链表
ListNode p1 = reverse(head1);
ListNode p2 = reverse(head2);
int approve = 0;
int val = 0;
ListNode ret = new ListNode(1);
ListNode cur = ret;
// 2: 若对应的两个节点都不为空则相加并记录进位,最后用结果创建节点并串在结果链表中
while(p1 != null && p2 != null){
val = (p1.val + p2.val + approve) % 10;
approve = (p1.val + p2.val + approve) / 10;
cur.next = new ListNode(val);
cur = cur.next;
p1 = p1.next;
p2 = p2.next;
}
while(p1 != null){
val = (p1.val + approve) % 10;
approve = (p1.val + approve) / 10;
cur.next = new ListNode(val);
cur = cur.next;
p1 = p1.next;
}
while(p2 != null){
val = (p2.val + approve) % 10;
approve = (p2.val + approve) / 10;
cur.next = new ListNode(val);
cur = cur.next;
p2 = p2.next;
}
// 3: 算到最后若还有进位则添加一个值为1的节点
if(approve == 1){
cur.next = new ListNode(1);
}
// 4: 再次反转构造最终结果
return reverse(ret.next);
}
public ListNode reverse(ListNode head){
ListNode cur = head;
ListNode prev = null;
while(cur != null){
ListNode nextNode = cur.next;
cur.next = prev;
prev = cur;
cur = nextNode;
}
return prev;
}
}
解法2
public class Solution {
/** * * @param head1 ListNode类 * @param head2 ListNode类 * @return ListNode类 */
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
// 1: 创建两个栈并将两个链表分别放入栈中
Stack<Integer> stack1=new Stack();
Stack<Integer> stack2=new Stack();
int cnt=0;
int sum=0;
ListNode dummy=new ListNode(0);
while(head1!=null){
stack1.add(head1.val);
head1=head1.next;
}
while(head2!=null){
stack2.add(head2.val);
head2=head2.next;
}
// 2: 同时取出两个栈的元素并相加,最后连接到结果链表中
while(!stack1.isEmpty()||!stack2.isEmpty()||cnt>0){
int c=!stack1.isEmpty()?stack1.pop():0;
int d=!stack2.isEmpty()?stack2.pop():0;
sum=(c+d+cnt)%10;
cnt=(c+d+cnt)/10;
ListNode node=new ListNode(sum);
// 这里使用头插的方式进行连接省去了最后再反转的过程
node.next=dummy.next;
dummy.next=node;
}
return dummy.next;
}
}
边栏推荐
- kotlin:out in
- 我的创作纪念日 -- 2022年7月25日
- 【物理应用】大气吸收损耗附matlab代码
- Is the software testing industry really saturated?
- LeetCode_ 1137_ Nth teponacci number
- C and SQL mixed programming, vs need to download what things
- PyG搭建异质图注意力网络HAN实现DBLP节点预测
- [GXYCTF2019]StrongestMind
- Software testing dry goods
- 4 年后,Debian 终夺回“debian.community”域名!
猜你喜欢

历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...

What if you don't understand the difference between modularity, componentization and plug-in?

Why did wechat change from "small and beautiful" to "big and fat" when it expanded 575 times in 11 years?

Getting started with gateway

The open source of "avoiding disease and avoiding medicine" will not go far

Introduction and advanced level of MySQL (I)

数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开

视频融合云服务EasyCVR平台白名单功能如何使用?

SwiftUI 组件之如何实现电话号码掩码隐藏部分的文本字段TextField(教程含源码)

LeetCode_ 96_ Different binary search trees
随机推荐
“讳疾忌医”的开源走不远
QT running image
Is the software testing industry really saturated?
历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...
If you want to learn software testing, where can you learn zero foundation?
Is there any prospect and way out for software testing?
Kali doesn't have an eth0 network card? What if you don't connect to the Internet
kotlin:Nothing
How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?
Software testing dry goods
LeetCode_ 343_ integer partition
Win11电脑摄像头打开看不见,显示黑屏如何解决?
3、 Uni app fixed or direct to a certain page
面试官:ThreadLocal使用场景有哪些?内存泄露问题如何避免?
How to break through the bottleneck of professional development for software testing engineers
N32 replaces STM32. Don't ignore these details!
2022杭电多校第二场1011 DOS Card(线段树)
@The difference between Autowired and @resource
Introduction and advanced MySQL (7)
Getting started with gateway