From 585c76a3f49a83b516e9abddc7f850bea809f495 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Sat, 8 Oct 2022 14:09:39 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20add=20141?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\345\217\214\346\214\207\351\222\210.md" | 1 + "Index/\351\223\276\350\241\250.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 86 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 "LeetCode/141-150/141. \347\216\257\345\275\242\351\223\276\350\241\250\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\345\217\214\346\214\207\351\222\210.md" "b/Index/\345\217\214\346\214\207\351\222\210.md" index 50dac00e..73b95b28 100644 --- "a/Index/\345\217\214\346\214\207\351\222\210.md" +++ "b/Index/\345\217\214\346\214\207\351\222\210.md" @@ -11,6 +11,7 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [75. 颜色分类](https://leetcode.cn/problems/sort-colors/) | [LeetCode 题解链接](https://leetcode.cn/problems/sort-colors/solution/by-ac_oier-7lwk/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | +| [141. 环形链表](https://leetcode.cn/problems/linked-list-cycle/) | [LeetCode 题解链接](https://leetcode.cn/problems/linked-list-cycle/solution/by-ac_oier-lfgr/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/Index/\351\223\276\350\241\250.md" "b/Index/\351\223\276\350\241\250.md" index d95ad6c1..41947f42 100644 --- "a/Index/\351\223\276\350\241\250.md" +++ "b/Index/\351\223\276\350\241\250.md" @@ -11,6 +11,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | +| [141. 环形链表](https://leetcode.cn/problems/linked-list-cycle/) | [LeetCode 题解链接](https://leetcode.cn/problems/linked-list-cycle/solution/by-ac_oier-lfgr/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | diff --git "a/LeetCode/141-150/141. \347\216\257\345\275\242\351\223\276\350\241\250\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/141-150/141. \347\216\257\345\275\242\351\223\276\350\241\250\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..9ea90b6f --- /dev/null +++ "b/LeetCode/141-150/141. \347\216\257\345\275\242\351\223\276\350\241\250\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,86 @@ +### 题目描述 + +这是 LeetCode 上的 **[141. 环形链表](https://leetcode.cn/problems/linked-list-cycle/solution/by-ac_oier-lfgr/)** ,难度为 **简单**。 + +Tag : 「链表」、「快慢指针」、「双指针」 + + + +给你一个链表的头节点 `head`,判断链表中是否有环。 + +如果链表中有某个节点,可以通过连续跟踪 `next` 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 `pos` 来表示链表尾连接到链表中的位置(索引从 $0$ 开始)。注意:`pos` 不作为参数进行传递 。仅仅是为了标识链表的实际情况。 + +如果链表中存在环 ,则返回 true 。 否则,返回 false 。 + +示例 1: +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist.png) +``` +输入:head = [3,2,0,-4], pos = 1 + +输出:true + +解释:链表中有一个环,其尾部连接到第二个节点。 +``` +示例 2: +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test2.png) +``` +输入:head = [1,2], pos = 0 + +输出:true + +解释:链表中有一个环,其尾部连接到第一个节点。 +``` +示例 3: +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test3.png) +``` +输入:head = [1], pos = -1 + +输出:false + +解释:链表中没有环。 +``` + +提示: +* 链表中节点的数目范围是 $[0, 10^4]$ +* $-10^5 <= Node.val <= 10^5$ +* `pos` 为 `-1` 或者链表中的一个 有效索引 。 + +进阶:你能用 $O(1)$(即,常量)内存解决此问题吗? + +--- + +### 快慢指针 + +这是一道「快慢指针」的模板题。 + +使用两变量 `slow` 和 `fast` 分别代表快慢指针,`slow` 每次往后走一步,而 `fast` 每次往后两步,两者初始化均为 `head`。 + +若链表无环,则 `fast` 必然会先走到结尾,算法正常结束;若链表有环,当 `slow` 来到环入口时,`fast` 必然已经在环中的某个位置,此时再继续进行,由于存在速度差,发生相遇必不可能是 `slow` 追上 `fast`,而只能是 `fast` 从后方追上 `slow`,由于速度差为 $1$,因此最多会消耗不超过环节点个数的回合,两者即会相遇。 + +代码: +```Java +public class Solution { + public boolean hasCycle(ListNode head) { + ListNode slow = head, fast = head; + while (fast != null && fast.next != null) { + slow = slow.next; fast = fast.next.next; + if (slow == fast) return true; + } + return false; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.141` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。