Skip to content

Commit 096d624

Browse files
committed
leetcode 160 补充js代码
1 parent c09b621 commit 096d624

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

animation-simulation/链表篇/leetcode相交链表.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ public:
7979
};
8080
```
8181
82+
JS Code:
83+
```javascript
84+
var getIntersectionNode = function(headA, headB) {
85+
let tempa = headA, tempb = headB
86+
const map = new Map()
87+
while(tempa){
88+
map.set(tempa, 1)
89+
tempa = tempa.next
90+
}
91+
while(tempb){
92+
if(map.get(tempb))
93+
return tempb
94+
tempb = tempb.next
95+
}
96+
return tempb
97+
};
98+
```
99+
82100
下面这个方法比较巧妙,不是特别容易想到,大家可以自己实现一下,这个方法也是利用我们的双指针思想。
83101

84102
下面我们直接看动图吧,特别直观,一下就可以搞懂。
@@ -128,6 +146,18 @@ public:
128146
};
129147
```
130148
149+
JS Code:
150+
```javascript
151+
var getIntersectionNode = function(headA, headB) {
152+
let tempa = headA, tempb = headB
153+
while(tempa !== tempb){
154+
tempa = tempa ? tempa.next : headB
155+
tempb = tempb ? tempb.next : headA
156+
}
157+
return tempa
158+
};
159+
```
160+
131161
好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。
132162

133163

0 commit comments

Comments
 (0)