mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-24 13:03:41 +00:00
添加py和js,添加注释
This commit is contained in:
parent
dff62af40a
commit
ef1da6bf6f
@ -10,7 +10,7 @@
|
||||
|
||||
今天给大家带来一个不是那么难的题目,这个题目的解答方法很多,只要能AC的就是好方法,虽然题目不是特别难但是也是剑指offer上的经典题目所以大家要记得打卡呀。
|
||||
|
||||
然后今天我们的链表板块就算结束啦。周末的时候我会对链表的题目做一个总结,俗话说温故而知新嘛。好啦废话不多说,我们一起来看一下今天的题目吧
|
||||
然后今天我们的链表板块就算结束啦。周末的时候我会对链表的题目做一个总结,俗话说温故而知新嘛。好啦废话不多说,我们一起来看一下今天的题目吧!
|
||||
|
||||
题目描述:
|
||||
|
||||
@ -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()
|
||||
# 遍历链表A,将所有值都存到arr中
|
||||
while tempa is not None:
|
||||
arr.add(tempa)
|
||||
tempa = tempa.next
|
||||
# 遍历列表B,如果发现某个结点已在arr中则直接返回该节点
|
||||
while tempb is not None:
|
||||
if tempb in arr:
|
||||
return tempb
|
||||
tempb = tempb.next
|
||||
# 若上方没有返回,此刻tempb为null
|
||||
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也行
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -128,11 +183,44 @@ public:
|
||||
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也行
|
||||
```
|
||||
|
||||
好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user