添加py和js,添加注释

pull/33/head
jaredliw 2021-07-12 19:35:02 +08:00
parent dff62af40a
commit ef1da6bf6f
1 changed files with 92 additions and 4 deletions

View File

@ -10,7 +10,7 @@
ACoffer
@ -41,16 +41,19 @@ public class Solution {
ListNode tempb = headB;
//定义Hashset
HashSet<ListNode> arr = new HashSet<ListNode>();
//遍历链表A将所有值都存到arr中
while (tempa != null) {
arr.add(tempa);
tempa = tempa.next;
}
//遍历列表B如果发现某个结点已在arr中则直接返回该节点
while (tempb != null) {
if (arr.contains(tempb)) {
return tempb;
}
tempb = tempb.next;
}
//若上方没有返回此刻tempb为null
return tempb;
}
@ -67,21 +70,73 @@ public:
ListNode * tempb = headB;
//定义Hashset
set <ListNode *> arr;
//遍历链表A将所有值都存到arr中
while (tempa != nullptr) {
arr.insert(tempa);
tempa = tempa->next;
}
//遍历列表B如果发现某个结点已在arr中则直接返回该节点
while (tempb != nullptr) {
if (arr.find(tempb) != arr.end()) {
return tempb;
}
tempb = tempb->next;
}
//若上方没有返回此刻tempb为null
return tempb;
}
};
```
JS Code:
```js
var getIntersectionNode = function(headA, headB) {
let tempa = headA;
let tempb = headB;
//定义Hashset
let arr = new Set();
//遍历链表A将所有值都存到arr中
while (tempa) {
arr.add(tempa);
tempa = tempa.next;
}
//遍历列表B如果发现某个结点已在arr中则直接返回该节点
while (tempb) {
if (arr.has(tempb)) {
return tempb;
}
tempb = tempb.next;
}
//若上方没有返回此刻tempb为null
return tempb;
};
```
Python Code:
```py
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
tempa = headA
tempb = headB
# Hashset
arr = set()
# Aarr
while tempa is not None:
arr.add(tempa)
tempa = tempa.next
# Barr
while tempb is not None:
if tempb in arr:
return tempb
tempb = tempb.next
# tempbnull
return tempb
```
@ -108,7 +163,7 @@ public class Solution {
tempa = tempa != null ? tempa.next: headB;
tempb = tempb != null ? tempb.next: headA;
}
return tempa;
return tempa;//返回tempb也行
}
}
```
@ -124,15 +179,48 @@ public:
ListNode * tempb = headB;
//循环
while (tempa != tempb) {
//如果不为空就指针下移,为空就跳到另一链表的头部
//如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != nullptr ? tempa->next: headB;
tempb = tempb != nullptr ? tempb->next: headA;
}
return tempa;
return tempa;//返回tempb也行
}
};
```
JS Code:
```js
var getIntersectionNode = function(headA, headB) {
//定义两个节点
let tempa = headA;
let tempb = headB;
//循环
while (tempa != tempb) {
//如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != null ? tempa.next: headB;
tempb = tempb != null ? tempb.next: headA;
}
return tempa;//返回tempb也行
};
```
Python Code:
```py
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
#
tempa = headA
tempb = headB
#
while tempa is not tempb:
#
tempa = tempa.next if tempa is not None else headB
tempb = tempb.next if tempb is not None else headA
return tempa # tempb
```