添加 面试题02.07.链表相交.md JavaScript语言解法的注释

This commit is contained in:
jobinben
2022-02-06 18:36:14 +08:00
parent 08e226bb00
commit f9e5bce7d7

View File

@ -224,12 +224,14 @@ var getIntersectionNode = function(headA, headB) {
lenA = getListLen(headA), lenA = getListLen(headA),
lenB = getListLen(headB); lenB = getListLen(headB);
if(lenA < lenB) { if(lenA < lenB) {
// 下面交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时
// 如果不加分号,下面两条代码等同于一条代码: [curA, curB] = [lenB, lenA]
[curA, curB] = [curB, curA]; [curA, curB] = [curB, curA];
[lenA, lenB] = [lenB, lenA]; [lenA, lenB] = [lenB, lenA];
} }
let i = lenA - lenB; let i = lenA - lenB;
while(i-- > 0) { while(i-- > 0) {
curA = curA.next curA = curA.next;
} }
while(curA && curA !== curB) { while(curA && curA !== curB) {
curA = curA.next; curA = curA.next;