forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
160.intersection-of-two-linked-lists.js
62 lines (54 loc) · 1.26 KB
/
160.intersection-of-two-linked-lists.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @lc app=leetcode id=160 lang=javascript
*
* [160] Intersection of Two Linked Lists
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function(headA, headB) {
// 网上精妙的解法没看懂
// see : https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/49789/My-accepted-simple-and-shortest-C%2B%2B-code-with-comments-explaining-the-algorithm.-Any-comments-or-improvements
if (headA === null || headB === null) return null;
let lenA = 0;
let lenB = 0;
let curA = headA;
let curB = headB;
while(curA) {
curA = curA.next;
lenA += 1;
}
while(curB) {
curB = curB.next;
lenB += 1;
}
const gap = Math.abs(lenA - lenB);
let cur = 0;
curA = headA;
curB = headB;
if (lenA > lenB) {
while(cur < gap) {
cur++;
curA = curA.next;
}
} else {
while(cur < gap) {
cur++;
curB = curB.next;
}
}
while(curA !== curB) {
curA = curA.next;
curB = curB.next;
}
return curA;
};