From 096d624df30d72edfbb9abc938c73ac91e4cd638 Mon Sep 17 00:00:00 2001 From: daluozha <561890199@qq.com> Date: Mon, 3 May 2021 22:49:26 +0800 Subject: [PATCH] =?UTF-8?q?leetcode=20160=20=E8=A1=A5=E5=85=85js=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../链表篇/leetcode相交链表.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/animation-simulation/链表篇/leetcode相交链表.md b/animation-simulation/链表篇/leetcode相交链表.md index da3dc51..2f84214 100644 --- a/animation-simulation/链表篇/leetcode相交链表.md +++ b/animation-simulation/链表篇/leetcode相交链表.md @@ -79,6 +79,24 @@ public: }; ``` +JS Code: +```javascript +var getIntersectionNode = function(headA, headB) { + let tempa = headA, tempb = headB + const map = new Map() + while(tempa){ + map.set(tempa, 1) + tempa = tempa.next + } + while(tempb){ + if(map.get(tempb)) + return tempb + tempb = tempb.next + } + return tempb +}; +``` + 下面这个方法比较巧妙,不是特别容易想到,大家可以自己实现一下,这个方法也是利用我们的双指针思想。 下面我们直接看动图吧,特别直观,一下就可以搞懂。 @@ -128,6 +146,18 @@ public: }; ``` +JS Code: +```javascript +var getIntersectionNode = function(headA, headB) { + let tempa = headA, tempb = headB + while(tempa !== tempb){ + tempa = tempa ? tempa.next : headB + tempb = tempb ? tempb.next : headA + } + return tempa +}; +``` + 好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。