diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index 493438d7..6bc331b5 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -155,6 +155,42 @@ Python: Go: +javaScript: + +```js +/** + * @param {ListNode} headA + * @param {ListNode} headB + * @return {ListNode} + */ +var getListLen = function(head) { + let len = 0, cur = head; + while(cur) { + len++; + cur = cur.next; + } + return len; +} +var getIntersectionNode = function(headA, headB) { + let curA = headA,curB = headB, + lenA = getListLen(headA), + lenB = getListLen(headB); + if(lenA < lenB) { + [curA, curB] = [curB, curA]; + [lenA, lenB] = [lenB, lenA]; + } + let i = lenA - lenB; + while(i-- > 0) { + curA = curA.next + } + while(curA && curA !== curB) { + curA = curA.next; + curB = curB.next; + } + return curA; +}; +``` +