当前位置:网站首页>链表求和[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 链表求和
边栏推荐
- Eth data set download and related problems
- 深度之眼(三)——矩阵的行列式
- Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
- 871. Minimum refueling times
- 一年頂十年
- uniapp H5页面调用微信支付
- Timing / counter of 32 and 51 single chip microcomputer
- Win10 system uses pip to install juypter notebook process record (installed on a disk other than the system disk)
- AP and F107 data sources and processing
- Believe in yourself and finish the JVM interview this time
猜你喜欢

Smart trash can (V) - light up OLED

剑指 Offer 24. 反转链表

博客主题 “Text“ 夏日清新特别版

The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?

宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权

How to transfer business data with BorgWarner through EDI?

剑指 Offer 25. 合并两个排序的链表

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

綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東

Microservice architecture practice: Construction of highly available distributed file system fastdfs architecture
随机推荐
JS20 array flattening
ThreadLocal
求简单微分方程
13、Darknet YOLO3
Un an à dix ans
Leetcode question brushing record | 933_ Recent requests
Introduce the scrollintoview() method attribute in detail
Shutter: action feedback
LeetCode:1380. Lucky number in matrix -- simple
例题 非线性整数规划
Si446 usage record (I): basic data acquisition
Interpretation of key parameters in MOSFET device manual
ThreadLocal
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
What if the default browser cannot be set?
线性规划例题 投资的收益与风险
SAP Commerce Cloud Storefront 框架选型:Accelerator 还是 Spartacus?
Believe in yourself and finish the JVM interview this time
【Leetcode】14. Longest Common Prefix
Dstat use [easy to understand]