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

173 lines
5.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[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
HashsetHashset
****
Java Code
```java
public class Solution {
public ListNode getIntersectionNode (ListNode headA, ListNode headB) {
ListNode tempa = headA;
ListNode tempb = headB;
//定义Hashset
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;
}
return tempb;
}
}
```
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;
}
};
```
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
};
```
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/第一次相交的点.5nbxf5t3hgk0.gif)
****
Java Code
```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;
}
}
```
C++ Code
```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;
}
};
```
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
};
```