当前位置:网站首页>链表求和[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 链表求和
边栏推荐
- Connect Porsche and 3PL EDI cases
- After meeting a full stack developer from Tencent, I saw what it means to be proficient in MySQL tuning
- 剑指 Offer 26. 树的子结构
- Chmod command principle and usage details [easy to understand]
- Introduction to nexus and detailed tutorial of Xiaobai using idea to package and upload to nexus3 private server
- 博客主题 “Text“ 夏日清新特别版
- [essay solicitation activity] Dear developer, RT thread community calls you to contribute
- [shutter] dart data type (dynamic data type)
- chmod命令原理及用法详解[通俗易懂]
- Eth data set download and related problems
猜你喜欢

13、Darknet YOLO3

剑指 Offer 26. 树的子结构

Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan

从收集到输出:盘点那些强大的知识管理工具——优秀笔记软件盘点(四)

Sword finger offer 25 Merge two sorted linked lists
![[web technology] 1233 seconds understand web component](/img/42/c98d8112dc4ece0a92dda97647be5c.jpg)
[web technology] 1233 seconds understand web component

深度之眼(二)——矩阵及其基本运算

The construction of scalable distributed database cluster and the partition design of oneproxy sub database

Connect Porsche and 3PL EDI cases

Microservice architecture practice: Construction of scalable distributed database cluster
随机推荐
牛客JS2 文件扩展名
使用知行之桥的API端口,提供资源供合作伙伴访问
[shutter] dart data type (dynamic data type)
IPtables中SNAT、DNAT和MASQUERADE的含义
13、Darknet YOLO3
LeetCode:1380. Lucky number in matrix -- simple
Update iteration of cloud communication interface -- the official launch of subail API V4
Niuke js3 separator
si446使用记录(一):基本资料获取
Experience home office, feel the completion of the project | community essay solicitation
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst
Shutter: action feedback
Use of openpose
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
OpenHarmony如何启动FA(本地和远程)
Tech talk activity preview | building intelligent visual products based on Amazon kVs
ROS知识点——消息过滤器 ( message_filters)
相信自己,这次一把搞定JVM面试
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights