Skip to content

Commit ef1da6b

Browse files
committed
添加py和js,添加注释
1 parent dff62af commit ef1da6b

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

animation-simulation/链表篇/剑指Offer52两个链表的第一个公共节点.md

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
今天给大家带来一个不是那么难的题目,这个题目的解答方法很多,只要能AC的就是好方法,虽然题目不是特别难但是也是剑指offer上的经典题目所以大家要记得打卡呀。
1212

13-
然后今天我们的链表板块就算结束啦。周末的时候我会对链表的题目做一个总结,俗话说温故而知新嘛。好啦废话不多说,我们一起来看一下今天的题目吧
13+
然后今天我们的链表板块就算结束啦。周末的时候我会对链表的题目做一个总结,俗话说温故而知新嘛。好啦废话不多说,我们一起来看一下今天的题目吧
1414

1515
题目描述:
1616

@@ -41,16 +41,19 @@ public class Solution {
4141
ListNode tempb = headB;
4242
//定义Hashset
4343
HashSet<ListNode> arr = new HashSet<ListNode>();
44+
//遍历链表A,将所有值都存到arr中
4445
while (tempa != null) {
4546
arr.add(tempa);
4647
tempa = tempa.next;
4748
}
49+
//遍历列表B,如果发现某个结点已在arr中则直接返回该节点
4850
while (tempb != null) {
4951
if (arr.contains(tempb)) {
5052
return tempb;
5153
}
5254
tempb = tempb.next;
5355
}
56+
//若上方没有返回,此刻tempb为null
5457
return tempb;
5558

5659
}
@@ -67,21 +70,73 @@ public:
6770
ListNode * tempb = headB;
6871
//定义Hashset
6972
set <ListNode *> arr;
73+
//遍历链表A,将所有值都存到arr中
7074
while (tempa != nullptr) {
7175
arr.insert(tempa);
7276
tempa = tempa->next;
7377
}
78+
//遍历列表B,如果发现某个结点已在arr中则直接返回该节点
7479
while (tempb != nullptr) {
7580
if (arr.find(tempb) != arr.end()) {
7681
return tempb;
7782
}
7883
tempb = tempb->next;
7984
}
85+
//若上方没有返回,此刻tempb为null
8086
return tempb;
8187
}
8288
};
8389
```
8490
91+
JS Code:
92+
93+
```js
94+
var getIntersectionNode = function(headA, headB) {
95+
let tempa = headA;
96+
let tempb = headB;
97+
//定义Hashset
98+
let arr = new Set();
99+
//遍历链表A,将所有值都存到arr中
100+
while (tempa) {
101+
arr.add(tempa);
102+
tempa = tempa.next;
103+
}
104+
//遍历列表B,如果发现某个结点已在arr中则直接返回该节点
105+
while (tempb) {
106+
if (arr.has(tempb)) {
107+
return tempb;
108+
}
109+
tempb = tempb.next;
110+
}
111+
//若上方没有返回,此刻tempb为null
112+
return tempb;
113+
};
114+
```
115+
116+
Python Code:
117+
118+
```py
119+
class Solution:
120+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
121+
tempa = headA
122+
tempb = headB
123+
# 定义Hashset
124+
arr = set()
125+
# 遍历链表A,将所有值都存到arr中
126+
while tempa is not None:
127+
arr.add(tempa)
128+
tempa = tempa.next
129+
# 遍历列表B,如果发现某个结点已在arr中则直接返回该节点
130+
while tempb is not None:
131+
if tempb in arr:
132+
return tempb
133+
tempb = tempb.next
134+
# 若上方没有返回,此刻tempb为null
135+
return tempb
136+
```
137+
138+
139+
85140
下面这个方法比较巧妙,不是特别容易想到,大家可以自己实现一下,这个方法也是利用我们的双指针思想。
86141

87142
下面我们直接看动图吧,特别直观,一下就可以搞懂。
@@ -108,7 +163,7 @@ public class Solution {
108163
tempa = tempa != null ? tempa.next: headB;
109164
tempb = tempb != null ? tempb.next: headA;
110165
}
111-
return tempa;
166+
return tempa;//返回tempb也行
112167
}
113168
}
114169
```
@@ -124,15 +179,48 @@ public:
124179
ListNode * tempb = headB;
125180
//循环
126181
while (tempa != tempb) {
127-
//如果不为空就指针下移,为空就跳到另一链表的头部
182+
//如果不为空就指针下移,为空就跳到另一链表的头部
128183
tempa = tempa != nullptr ? tempa->next: headB;
129184
tempb = tempb != nullptr ? tempb->next: headA;
130185
}
131-
return tempa;
186+
return tempa;//返回tempb也行
187+
}
188+
};
189+
```
190+
191+
JS Code:
192+
193+
```js
194+
var getIntersectionNode = function(headA, headB) {
195+
//定义两个节点
196+
let tempa = headA;
197+
let tempb = headB;
198+
//循环
199+
while (tempa != tempb) {
200+
//如果不为空就指针下移,为空就跳到另一链表的头部
201+
tempa = tempa != null ? tempa.next: headB;
202+
tempb = tempb != null ? tempb.next: headA;
132203
}
204+
return tempa;//返回tempb也行
133205
};
134206
```
135207

208+
Python Code:
209+
210+
```py
211+
class Solution:
212+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
213+
# 定义两个节点
214+
tempa = headA
215+
tempb = headB
216+
# 循环
217+
while tempa is not tempb:
218+
# 如果不为空就指针下移,为空就跳到另一链表的头部
219+
tempa = tempa.next if tempa is not None else headB
220+
tempb = tempb.next if tempb is not None else headA
221+
return tempa # 返回tempb也行
222+
```
223+
136224
好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。
137225

138226

0 commit comments

Comments
 (0)