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

294 lines
9.5 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>进入。
#### [ Offer 52. ](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/) & [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>();
//遍历链表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;
}
}
```
C++ Code:
```cpp
class Solution {
public:
ListNode * getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode * tempa = headA;
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:
```python
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
```
Swift Code
```swift
class Solution {
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
var tempa = headA
var tempb = headB
var arr:Set<ListNode> = []
//遍历链表A将所有值都存到arr中
while tempa != nil {
arr.insert(tempa!)
tempa = tempa?.next
}
//遍历列表B如果发现某个结点已在arr中则直接返回该节点
while tempb != nil {
if arr.contains(tempb!) {
return tempb
}
tempb = tempb?.next
}
//若上方没有返回此刻tempb为null
return tempb
}
}
extension ListNode: Hashable, Equatable {
public func hash(into hasher: inout Hasher) {
hasher.combine(val)
hasher.combine(ObjectIdentifier(self))
}
public static func ==(lhs: ListNode, rhs: ListNode) -> Bool {
return lhs === rhs
}
}
```
![](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;//返回tempb也行
}
}
```
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;//返回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:
```python
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
```
Swift Code
```swift
class Solution {
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
//定义两个节点
var tempa = headA
var tempb = headB
//循环
while tempa != tempb {
// 如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != nil ? tempa?.next : headB
tempb = tempb != nil ? tempb?.next : headA
}
return tempa //返回tempb也行
}
}
```
<br/>
> [@jaredliw](https://github.com/jaredliw)注:
>
>
>
> 1. kk
> 2.