当前位置:网站首页>List summation [dummy+ tail interpolation + function processing list reference common pit]
List summation [dummy+ tail interpolation + function processing list reference common pit]
2022-07-02 17:34:00 【REN_ Linsen】
dummy+ The tail interpolation
Preface
There are two ways to deal with linked lists , First of all , Is to directly operate the linked list , For simple cases ; second , use dummy As the head node of the new linked list , Insert the qualified nodes of the old linked list with the header / The tail interpolation method is connected to dummy/dummy-tail Back .
besides , When the function is passed in the sum of linked list node references , The reference variables of the current function and the calling function are the variables on the two stack frames , Not the same .
One 、 Sum of linked list
Two 、dummy+ The tail interpolation
package everyday.medium;
// Sum of linked list
public class AddTwoNumbers {
/* target: utilize dummy, Insert the generated node in the way of tail interpolation . The core of list summation : If you write a function to handle the remaining long linked list , The tail interpolation method will appear tail When the pointer does not move , because Java Only pass value , Two tail There are already two stack variables . */
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// With dummy As a new linked list
ListNode dummy = new ListNode(0);
// The tail interpolation
ListNode tail = dummy;
// carry
int plus = 0;
// Sum and carry
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;
}
// Handle long linked lists that are not empty .
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;
}
// By the way, the plus Not for 0 The situation is handled , Otherwise, you need to tail/plus All back to , notes : there tail It's value passing , And the function above tail It's already two variables .
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;
}
}
}
summary
1)dummy+ Tail interpolation operation linked list , Unified operation .
2) Pass a reference to the function , Then the reference of the two functions is different , A new reference variable memory is allocated on the stack frame of the new function , Used to point to the heap address .
reference
边栏推荐
猜你喜欢
About me
13、Darknet YOLO3
A case study of college entrance examination prediction based on multivariate time series
Timing / counter of 32 and 51 single chip microcomputer
si446使用记录(一):基本资料获取
QStyle实现自绘界面项目实战(二)
例题 非线性整数规划
Qstype implementation of self drawing interface project practice (II)
Platform management background and business menu resource management: business permissions and menu resource management design
Chrome browser quick access stackoverflow
随机推荐
Common SQL statements (complete example)
微信小程序 —— 上下浮动的箭头
uva1169
13、Darknet YOLO3
智能垃圾桶(五)——点亮OLED
PCL知识点——体素化网格方法对点云进行下采样
Explanation of traceroute command
Navigateur Chrome pour un accès rapide au stackoverflow
Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
SAP commerce cloud storefront framework selection: accelerator or Spartacus?
MATLAB中nexttile函数使用
Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
HBuilderX运行到手机或模拟器提示没有找到设备
si446使用记录(二):使用WDS3生成头文件
牛客JS2 文件扩展名
【目标跟踪】|数据集汇总
dstat使用[通俗易懂]
class和getClass()的区别
A few lines of code to complete RPC service registration and discovery
AtCoder Beginner Contest 237 VP补题