algorithm-base/animation-simulation/链表篇/leetcode142环形链表2.md

297 lines
10 KiB
Java
Raw Normal View History

2021-03-20 08:57:12 +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>进入。
#### [142. II](https://leetcode-cn.com/problems/linked-list-cycle-ii/)
2021-03-19 07:07:33 +00:00
![image-20201027175552961](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/image-20201027175552961.63sz69rbes00.png)
```java
public class Solution {
public boolean hasCycle(ListNode head) {
//特殊情况,无节点或只有一个节点的情况
if(head == null || head.next == null){
return false;
}
//设置快慢指针
ListNode pro = head.next;
ListNode last = head;
//循环条件
while( pro != null && pro.next!=null){
pro=pro.next.next;
last=last.next;
//两指针相遇
if(pro == last){
return true;
}
}
//循环结束,指针没有相遇,说明没有环。相当于快指针遍历了一遍链表
return false;
}
}
```
2021-07-14 15:32:30 +00:00
[](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode141%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.md)。
2021-03-19 07:07:33 +00:00
绿
绿2
![image-20201027180921770](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/image-20201027180921770.21fh8pt3cuv4.png)
### HashSet
HashSetHashSetHashSetHashSet
0123456**2 **2
![image-20201027182649669](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/image-20201027182649669.2g8gq4ik6xs0.png)
2021-07-14 15:32:30 +00:00
Java Code:
2021-03-19 07:07:33 +00:00
```java
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) {
return head;
}
if (head.next == null) {
return head.next;
}
2021-07-14 15:32:30 +00:00
//创建新的HashSet用于保存节点
2021-03-19 07:07:33 +00:00
HashSet<ListNode> hash = new HashSet<ListNode>();
//遍历链表
while (head != null) {
2021-07-14 15:32:30 +00:00
//判断哈希表中是否含有某节点,没有则保存,含有则返回该节点
2021-03-19 07:07:33 +00:00
if (hash.contains(head)) {
return head;
}
//不含有,则进行保存,并移动指针
hash.add(head);
head = head.next;
}
return head;
}
}
```
2021-07-14 15:32:30 +00:00
C++ Code:
```cpp
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (head == nullptr) return head;
if (head->next == nullptr) return head->next;
set<ListNode *> hash;
while (head != nullptr) {
if (hash.count(head)) {
return head;
}
hash.insert(head);
head = head->next;
}
return head;
}
};
```
JS Code:
```javascript
var detectCycle = function(head) {
if (head === null) return head;
if (head.next === null) return head.next;
//创建新的HashSet用于保存节点
let hash = new Set();
//遍历链表
while (head !== null) {
//判断哈希表中是否含有某节点,没有则保存,含有则返回该节点
if (hash.has(head)) {
return head;
}
//不含有,则进行保存,并移动指针
hash.add(head);
head = head.next;
}
return head;
};
```
Python Code:
```py
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
if head is None:
return head
if head.next is None:
return head.next
# HashSet
hash = set()
while head is not None:
#
if head in hash:
return head
#
hash.add(head)
head = head.next
return head
```
2021-03-19 07:07:33 +00:00
###
![image-20201027184755943](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/image-20201027184755943.3edot8s2xi60.png)
2021-07-14 15:32:30 +00:00
**a+(b+c)n+b**n
2021-03-19 07:07:33 +00:00
b+ca线bna+(b+c)n+ba+(n+1)b+nc
2021-07-14 15:32:30 +00:00
**a+(b+c)m+b**m
2021-03-19 07:07:33 +00:00
2021-07-14 15:32:30 +00:00
22
2021-03-19 07:07:33 +00:00
2021-07-14 15:32:30 +00:00
**a+(n+1)b+nc=2[a+(m+1)b+mc]****a+b=(n-2m)(b+c)**
2021-03-19 07:07:33 +00:00
2021-07-14 15:32:30 +00:00
**b+c**a+bn-2m便n-2m1a+b=b+ca=cheada=c
2021-03-19 07:07:33 +00:00
2021-07-14 15:32:30 +00:00
1.2
2021-03-19 07:07:33 +00:00
2021-07-14 15:32:30 +00:00
2.
2021-03-19 07:07:33 +00:00
2021-07-14 15:32:30 +00:00
3.head1
2021-03-19 07:07:33 +00:00
![2](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/环形链表2.elwu1pw2lw0.gif)
2021-04-28 10:28:00 +00:00
****
2021-03-19 07:07:33 +00:00
2021-04-28 10:28:00 +00:00
Java Code:
2021-03-19 07:07:33 +00:00
```java
public class Solution {
public ListNode detectCycle(ListNode head) {
//快慢指针
ListNode fast = head;
2021-07-15 03:24:37 +00:00
ListNode slow = head;
2021-03-19 07:07:33 +00:00
//设置循环条件
while (fast != null && fast.next != null) {
fast = fast.next.next;
2021-07-15 03:24:37 +00:00
slow = slow.next;
2021-03-19 07:07:33 +00:00
//相遇
2021-07-15 03:24:37 +00:00
if (fast == slow) {
2021-03-19 07:07:33 +00:00
//设置一个新的指针从头节点出发慢指针速度为1所以可以使用慢指针从相遇点出发
2021-07-15 03:24:37 +00:00
ListNode newptr = head;
while (newptr != slow) {
slow = slow.next;
newptr = newptr.next;
2021-03-19 07:07:33 +00:00
}
//在环入口相遇
2021-07-15 03:24:37 +00:00
return slow;
2021-03-19 07:07:33 +00:00
}
}
return null;
}
}
```
2021-04-28 10:28:00 +00:00
C++ Code:
```cpp
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
//快慢指针
ListNode * fast = head;
2021-07-15 03:24:37 +00:00
ListNode * slow = head;
2021-04-28 10:28:00 +00:00
//设置循环条件
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
2021-07-15 03:24:37 +00:00
slow = slow->next;
2021-04-28 10:28:00 +00:00
//相遇
2021-07-15 03:24:37 +00:00
if (fast == slow) {
2021-04-28 10:28:00 +00:00
//设置一个新的指针从头节点出发慢指针速度为1所以可以使用慢指针从相遇点出发
ListNode * newnode = head;
2021-07-15 03:24:37 +00:00
while (newnode != slow) {
slow = slow->next;
2021-04-28 10:28:00 +00:00
newnode = newnode->next;
}
//在环入口相遇
2021-07-15 03:24:37 +00:00
return slow;
2021-04-28 10:28:00 +00:00
}
}
return nullptr;
}
};
```
2021-07-15 03:24:37 +00:00
JS Code:
```js
var detectCycle = function(head) {
let fast = head;
let slow = head;
while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
let newptr = head;
while (newptr != slow) {
slow = slow.next;
newptr = newptr.next;
}
return slow;
}
}
return null;
};
```
Python Code:
```py
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
#
fast = head
slow = head
#
while fast is not None and fast.next is not None:
fast = fast.next.next
slow = slow.next
#
if fast is slow:
# 1使
newptr = head
while newptr is not slow:
slow = slow.next
newptr = newptr.next
#
return slow
```