当前位置:网站首页>链表求和[dummy+尾插法+函数处理链表引用常见坑位]
链表求和[dummy+尾插法+函数处理链表引用常见坑位]
2022-07-02 15:03:00 【REN_林森】
dummy+尾插法
前言
处理链表有两种方式,第一,则是直接操作链表,适用于简单情况;第二,采用dummy做为新链表的头节点,把旧链表符合条件的节点以头插法/尾插法的方式接到dummy/dummy-tail后面。
除此之外,当给函数传入链表节点引用之和,当前函数的引用变量和调用函数的引用变量就是两个栈帧上的变量了,不是同一个了。
一、链表求和
二、dummy+尾插法
package everyday.medium;
// 链表求和
public class AddTwoNumbers {
/* target:利用dummy,将生成的节点以尾插法的方式插入。 链表求和的核心点:如果写函数处理剩余的长链表,则会出现尾插法tail指针不移动的情况,因为Java只有传值,两个tail已经是两个栈变量了。 */
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// 以dummy来作为新链表
ListNode dummy = new ListNode(0);
// 尾插法
ListNode tail = dummy;
// 进位
int plus = 0;
// 求和并进位
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + plus;
int val = sum % 10;
ListNode n = new ListNode(val);
tail.next = n;
tail = n;
// for cycle
l1 = l1.next;
l2 = l2.next;
plus = sum / 10;
}
// 处理不为空的长链表。
processLongList(tail, l1 == null ? l2 : l1, plus);
return dummy.next;
}
//
private void processLongList(ListNode tail, ListNode l, int plus) {
while (l != null) {
int sum = l.val + plus;
int val = sum % 10;
ListNode n = new ListNode(val);
tail.next = n;
tail = n;
l = l.next;
plus = sum / 10;
}
// 顺便把plus 不为0的情况处理了,不然就需要把tail/plus都返回,注:这里的tail是传值,和上面函数那个tail已经是两个变量了。
if (plus != 0) tail.next = new ListNode(plus);
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
}
总结
1)dummy+尾插法操作链表,统一操作。
2)给函数传入引用,则两个函数的引用就不同了,新函数的栈帧上分配了新的引用变量内存,用于指向堆地址。
参考文献
[1] LeetCode 链表求和
边栏推荐
- The impact of telecommuting on all aspects of our experience | community essay solicitation
- 一年顶十年
- Sword finger offer 22 The penultimate node in the linked list
- 2022 interview questions
- VScode知识点——常见报错
- Leetcode question brushing record | 933_ Recent requests
- 人生的开始
- Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
- si446使用记录(二):使用WDS3生成头文件
- Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei
猜你喜欢
Tech talk activity preview | building intelligent visual products based on Amazon kVs
线性规划例题 投资的收益与风险
ETH数据集下载及相关问题
What if the default browser cannot be set?
剑指 Offer 26. 树的子结构
Microservice architecture practice: Construction of scalable distributed database cluster
伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
使用知行之桥的API端口,提供资源供合作伙伴访问
Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights
Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei
随机推荐
Eye of depth (II) -- matrix and its basic operations
Meanings of SNAT, DNAT and masquerade in iptables
Exploration of mobile application performance tools
QWebEngineView崩溃及替代方案
What if the default browser cannot be set?
executescalar mysql_ ExecuteScalar()
剑指 Offer 24. 反转链表
Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
A few lines of code to complete RPC service registration and discovery
体验居家办公完成项目有感 | 社区征文
Use of openpose
JS20 数组扁平化
AP and F107 data sources and processing
Update iteration of cloud communication interface -- the official launch of subail API V4
Sword finger offer 27 Image of binary tree
【Leetcode】14. 最長公共前綴
2022 interview questions
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
ROS knowledge points -- the difference between ros:: nodehandle N and NH ("~")
云通信接口更新迭代——SUBMAIL API V4正式上线