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

173 lines
5.4 KiB
Java
Raw Normal View History

2021-03-21 04:42:30 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [160. ](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/)
###
ACoffer
![image-20201029215837844](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/image-20201029215837844.7ezoerpghyk0.png)
HashSet
### HashSet
2021-04-28 10:28:00 +00:00
HashsetHashset
****
Java Code
2021-03-21 04:42:30 +00:00
```java
public class Solution {
public ListNode getIntersectionNode (ListNode headA, ListNode headB) {
ListNode tempa = headA;
ListNode tempb = headB;
2021-04-28 10:28:00 +00:00
//定义Hashset
2021-03-21 04:42:30 +00:00
HashSet<ListNode> arr = new HashSet<ListNode>();
while (tempa != null) {
arr.add(tempa);
tempa = tempa.next;
}
while (tempb != null) {
if (arr.contains(tempb)) {
return tempb;
}
tempb = tempb.next;
}
2021-04-28 10:28:00 +00:00
return tempb;
2021-03-21 04:42:30 +00:00
}
}
```
2021-04-28 10:28:00 +00:00
C++ Code:
```cpp
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode * tempa = headA;
ListNode * tempb = headB;
//定义Hashset, cpp对应set
set <ListNode *> arr;
while (tempa != nullptr) {
arr.insert(tempa);
tempa = tempa->next;
}
while (tempb != nullptr) {
if (arr.find(tempb) != arr.end()) {
return tempb;
}
tempb = tempb->next;
}
return tempb;
}
};
```
2021-05-03 14:49:26 +00:00
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
};
```
2021-03-21 04:42:30 +00:00
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/第一次相交的点.5nbxf5t3hgk0.gif)
2021-04-28 10:28:00 +00:00
****
Java Code
2021-03-21 04:42:30 +00:00
```java
public class Solution {
public ListNode getIntersectionNode (ListNode headA, ListNode headB) {
//定义两个节点
ListNode tempa = headA;
ListNode tempb = headB;
//循环
while (tempa != tempb) {
//如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != null ? tempa.next:headB;
tempb = tempb != null ? tempb.next:headA;
}
return tempa;
}
}
```
2021-04-28 10:28:00 +00:00
C++ Code
2021-03-21 04:42:30 +00:00
2021-04-28 10:28:00 +00:00
```cpp
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
//定义两个节点
ListNode * tempa = headA;
ListNode * tempb = headB;
//循环
while (tempa != tempb) {
//如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != nullptr ? tempa->next: headB;
tempb = tempb != nullptr ? tempb->next: headA;
}
return tempa;
}
};
```
2021-03-21 04:42:30 +00:00
2021-05-03 14:49:26 +00:00
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
};
```
2021-03-21 04:42:30 +00:00