Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9aefdb7

Browse files
Merge pull request SharingSource#705 from SharingSource/ac_oier
✨feat: add 141
2 parents 923b5f2 + 585c76a commit 9aefdb7

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| [45. 跳跃游戏 II](https://leetcode-cn.com/problems/jump-game-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-jie-dp-tan-xin-shuang-zhi-zhen-jie-roh4/) | 中等 | 🤩🤩🤩🤩 |
1212
| [75. 颜色分类](https://leetcode.cn/problems/sort-colors/) | [LeetCode 题解链接](https://leetcode.cn/problems/sort-colors/solution/by-ac_oier-7lwk/) | 中等 | 🤩🤩🤩🤩 |
1313
| [88. 合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/merge-sorted-array/solution/gong-shui-san-xie-yi-ti-san-jie-shuang-z-47gj/) | 简单 | 🤩🤩🤩 |
14+
| [141. 环形链表](https://leetcode.cn/problems/linked-list-cycle/) | [LeetCode 题解链接](https://leetcode.cn/problems/linked-list-cycle/solution/by-ac_oier-lfgr/) | 简单 | 🤩🤩🤩🤩 |
1415
| [345. 反转字符串中的元音字母](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/solution/gong-shui-san-xie-note-bie-pian-shuang-z-c8ii/) | 简单 | 🤩🤩🤩 |
1516
| [395. 至少有 K 个重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/solution/xiang-jie-mei-ju-shuang-zhi-zhen-jie-fa-50ri1/) | 中等 | 🤩🤩🤩 |
1617
| [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 |

‎Index/链表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| [82. 删除排序链表中的重复元素 II](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/tong-yong-shan-chu-zhong-fu-jie-dian-lia-od9g/) | 简单 | 🤩🤩🤩🤩🤩 |
1212
| [92. 反转链表 II](https://leetcode-cn.com/problems/reverse-linked-list-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-linked-list-ii/solution/yi-ge-neng-ying-yong-suo-you-lian-biao-t-vjx6/) | 中等 | 🤩🤩🤩 |
1313
| [138. 复制带随机指针的链表](https://leetcode-cn.com/problems/copy-list-with-random-pointer/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/copy-list-with-random-pointer/solution/gong-shui-san-xie-yi-ti-shuang-jie-ha-xi-pqek/) | 中等 | 🤩🤩🤩 |
14+
| [141. 环形链表](https://leetcode.cn/problems/linked-list-cycle/) | [LeetCode 题解链接](https://leetcode.cn/problems/linked-list-cycle/solution/by-ac_oier-lfgr/) | 简单 | 🤩🤩🤩🤩 |
1415
| [160. 相交链表](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/gong-shui-san-xie-zhao-liang-tiao-lian-b-h3bd/) | 简单 | 🤩🤩🤩🤩🤩 |
1516
| [146. LRU 缓存机制](https://leetcode-cn.com/problems/lru-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lru-cache/solution/gong-shui-san-xie-she-ji-shu-ju-jie-gou-68hv2/) | 中等 | 🤩🤩🤩🤩🤩 |
1617
| [203. 移除链表元素](https://leetcode-cn.com/problems/remove-linked-list-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-linked-list-elements/solution/gong-shui-san-xie-yi-chu-lian-biao-yuan-ca6fu/) | 简单 | 🤩🤩🤩 |
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[141. 环形链表](https://leetcode.cn/problems/linked-list-cycle/solution/by-ac_oier-lfgr/)** ,难度为 **简单**
4+
5+
Tag : 「链表」、「快慢指针」、「双指针」
6+
7+
8+
9+
给你一个链表的头节点 `head`,判断链表中是否有环。
10+
11+
如果链表中有某个节点,可以通过连续跟踪 `next` 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 `pos` 来表示链表尾连接到链表中的位置(索引从 0ドル$ 开始)。注意:`pos` 不作为参数进行传递 。仅仅是为了标识链表的实际情况。
12+
13+
如果链表中存在环 ,则返回 true 。 否则,返回 false 。
14+
15+
示例 1:
16+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist.png)
17+
```
18+
输入:head = [3,2,0,-4], pos = 1
19+
20+
输出:true
21+
22+
解释:链表中有一个环,其尾部连接到第二个节点。
23+
```
24+
示例 2:
25+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test2.png)
26+
```
27+
输入:head = [1,2], pos = 0
28+
29+
输出:true
30+
31+
解释:链表中有一个环,其尾部连接到第一个节点。
32+
```
33+
示例 3:
34+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test3.png)
35+
```
36+
输入:head = [1], pos = -1
37+
38+
输出:false
39+
40+
解释:链表中没有环。
41+
```
42+
43+
提示:
44+
* 链表中节点的数目范围是 $[0, 10^4]$
45+
* $-10^5 <= Node.val <= 10^5$
46+
* `pos``-1` 或者链表中的一个 有效索引 。
47+
48+
进阶:你能用 $O(1)$(即,常量)内存解决此问题吗?
49+
50+
---
51+
52+
### 快慢指针
53+
54+
这是一道「快慢指针」的模板题。
55+
56+
使用两变量 `slow``fast` 分别代表快慢指针,`slow` 每次往后走一步,而 `fast` 每次往后两步,两者初始化均为 `head`
57+
58+
若链表无环,则 `fast` 必然会先走到结尾,算法正常结束;若链表有环,当 `slow` 来到环入口时,`fast` 必然已经在环中的某个位置,此时再继续进行,由于存在速度差,发生相遇必不可能是 `slow` 追上 `fast`,而只能是 `fast` 从后方追上 `slow`,由于速度差为 1ドル,ドル因此最多会消耗不超过环节点个数的回合,两者即会相遇。
59+
60+
代码:
61+
```Java
62+
public class Solution {
63+
public boolean hasCycle(ListNode head) {
64+
ListNode slow = head, fast = head;
65+
while (fast != null && fast.next != null) {
66+
slow = slow.next; fast = fast.next.next;
67+
if (slow == fast) return true;
68+
}
69+
return false;
70+
}
71+
}
72+
```
73+
* 时间复杂度:$O(n)$
74+
* 空间复杂度:$O(1)$
75+
76+
---
77+
78+
### 最后
79+
80+
这是我们「刷穿 LeetCode」系列文章的第 `No.141` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
81+
82+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
83+
84+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
85+
86+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /