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

127 lines
3.7 KiB
Java
Raw Normal View History

2021-07-23 15:44:19 +00:00
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
2021-03-21 04:10:31 +00:00
>
> 感谢支持该仓库会一直维护希望对各位有一丢丢帮助
>
2021-07-23 15:44:19 +00:00
> 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-21 04:10:31 +00:00
### [141. 环形链表](https://leetcode-cn.com/problems/linked-list-cycle/)
下面我们再来了解一种双指针我们称之为快慢指针顾名思义一个指针速度快一个指针速度慢
#### 题目描述
2021-07-23 15:44:19 +00:00
> 给定一个链表判断链表中是否有环pos 代表环的入口若为-1则代表无环
2021-03-21 04:10:31 +00:00
>
2021-07-15 16:06:52 +00:00
> 如果链表中存在环则返回 true 否则返回 false
2021-03-21 04:10:31 +00:00
2021-07-23 15:44:19 +00:00
示例 1
2021-03-21 04:10:31 +00:00
2021-03-21 05:20:43 +00:00
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210321131949755.png)
2021-03-21 04:10:31 +00:00
> 输入head = [3,2,0,-4], pos = 1
> 输出true
> 解释链表中有一个环其尾部连接到第二个节点
#### 题目解析
题目很容易理解让我们判断链表中是否有环我们只需通过我们的快慢指针即可我们试想一下如果链表中有环的话一个速度快的指针和一个速度慢的指针在环中运动的话若干圈后快指针肯定可以追上慢指针的这是一定的
2021-03-21 05:20:43 +00:00
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210321132015849.png)
2021-03-21 04:10:31 +00:00
2021-07-13 13:06:03 +00:00
好啦做题思路已经有了让我们一起看一下代码的执行过程吧
2021-03-21 04:10:31 +00:00
**动画模拟**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210321115836276.gif)
**题目代码**
2021-04-27 10:14:29 +00:00
Java Code:
2021-07-23 15:44:19 +00:00
2021-03-21 04:10:31 +00:00
```java
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode low = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
low = low.next;
if (fast == low) {
return true;
}
}
return false;
}
}
```
2021-04-28 10:28:00 +00:00
C++ Code:
```cpp
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode * fast = head;
2021-07-13 13:06:03 +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-13 13:06:03 +00:00
slow = slow->next;
if (fast == slow) {
2021-04-28 10:28:00 +00:00
return true;
}
}
return false;
}
};
```
2021-07-15 16:06:52 +00:00
JS Code:
```javascript
2021-07-23 15:44:19 +00:00
var hasCycle = function (head) {
let fast = head;
let slow = head;
while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
if (fast === slow) {
return true;
2021-07-15 16:06:52 +00:00
}
2021-07-23 15:44:19 +00:00
}
return false;
2021-07-15 16:06:52 +00:00
};
```
2021-07-13 13:06:03 +00:00
Python Code:
2021-07-15 16:06:52 +00:00
```python
2021-07-13 13:06:03 +00:00
class Solution:
def hasCycle(self, head: ListNode) -> bool:
fast = head
slow = head
while fast and fast.next:
fast = fast.next.next
2021-07-15 16:06:52 +00:00
slow = slow.next
if fast == slow:
2021-07-13 13:06:03 +00:00
return True
return False
```
2021-07-17 14:28:06 +00:00
Swift Code
```swift
class Solution {
func hasCycle(_ head: ListNode?) -> Bool {
var fast = head, slow = head
while fast != nil && fast?.next != nil {
fast = fast?.next?.next
slow = slow?.next
if fast === slow {
return true
}
}
return false
}
}
```