[LC]141题 Linked List Cycle (环形链表)(链表)

①中文题目

给定一个链表,判断链表中是否有环。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

示例 1:

输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

输入:head = [1], pos = -1
输出:false
解释:链表中没有环。

②思路

我自己想到了用快慢指针,但是用错了,于是抄了官方题解,它的思路如下:

想象一下,两名运动员以不同的速度在环形赛道上跑步会发生什么?

通过使用具有 不同速度 的快、慢两个指针遍历链表,空间复杂度可以被降低至 O(1)O(1)。慢指针每次移动一步,而快指针每次移动两步。

如果列表中不存在环,最终快指针将会最先到达尾部,此时我们可以返回 false。

现在考虑一个环形链表,把慢指针和快指针想象成两个在环形赛道上跑步的运动员(分别称之为慢跑者与快跑者)。而快跑者最终一定会追上慢跑者。这是为什么呢?考虑下面这种情况(记作情况 A)- 假如    快跑者只落后慢跑者一步,在下一次迭代中,它们就会分别跑了一步或两步并相遇。

其他情况又会怎样呢?例如,我们没有考虑快跑者在慢跑者之后两步或三步的情况。但其实不难想到,因为在下一次或者下下次迭代后,又会变成上面提到的情况 A。

③代码

 1 public class Solution {
 2     public boolean hasCycle(ListNode head) {
 3         if (head == null || head.next == null) {
 4          return false;
 5         }
 6         ListNode slow = head;   //慢指针
 7         ListNode fast = head.next;   //快指针
 8         while (slow != fast) {
 9         if (fast == null || fast.next == null) {   //因为快指针1次走两步,所以很可能一步恰大了。
10             return false;                        //所以,需要fast == null || fast.next == null一起来判定
11         }
12         slow = slow.next;      //慢指针一次走1步,
13         fast = fast.next.next;  //快指针1次走2步,
14         }
15         return true;
16     }
17 }

④运算结果 通过

执行用时 :0 ms, 在所有 Java 提交中击败了100.00%的用户

内存消耗 :37.3 MB, 在所有 Java 提交中击败了96.99%的用户

⑤学到的知识

1、并不是只有

1 for(i=0;i...){
2    for(j=i;j...){
3          .....
4     }
5 }

这种叫快慢指针。而是如上述代码里的12,13行,让快的指针比慢指针每次多走1不,那也叫快慢之分。

2、我自己写的快慢指针里面,不知道怎么从while循环里出来,,,导致不知道慢指针什么时候后移1位。现在学到了

直接判断

while (slow != fast)

就是判断进、出循环的方法。

⑥ 这个题,官方解答里还有用哈希表的。我暂时用不来哈希表,准备以后再来用哈希表做做这个题。



原文地址:https://www.cnblogs.com/zf007/p/11609535.html

时间: 2024-07-29 12:37:25

[LC]141题 Linked List Cycle (环形链表)(链表)的相关文章

LeetCode | 0141. Linked List Cycle环形链表【Python】

LeetCode 0141. Linked List Cycle环形链表[Easy][Python][双指针] 题目 英文题目地址 Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked lis

[LeetCode]93. Linked List Cycle II查找链表中环的起始节点

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? Subscribe to see which companies asked this question 解法:这道题是题目L

[Leetcode] Linked list cycle ii 判断链表是否有环

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up:Can you solve it without using extra space? 题意:给定链表,若是有环,则返回环开始的节点,没有则返回NULL 思路:题目分两步走,第一.判断是否有环,第二若是有,找到环的起始点.关于第一点,可以参考之前的博客 Linked list cycle.

141 Linked List Cycle 环形链表

给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solut

[LC]141题 Intersection of Two Linked Lists (相交链表)(链表)

①中文题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 注意: 如果两个链表没有交点,返回 null.在返回结果后,两个链表仍须保持原有的结构.可假定整个链表结构中没有循环.程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存. ②思路 遍历,O(M*N)的遍历,直到找出相等的结点(不是val相等) ③我的代码(很慢) 1 public class Solution { 2 public ListNode getIntersectionNod

Linked List Cycle 判断一个链表是否存在回路(循环)

Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 注意,链表循环并不是尾指针和头指针相同,可能是在中间某一段形成一个环路,所以不能只判断元素和第一个元素是否存在重合 先设置两个指针p_fast和p_slow.从头开始遍历链表,p_fast每次走两个节点,而p_slow每次走一个节点,若存在循环,这两个指针必定重合: 1 /**

leetcode || 141、Linked List Cycle

problem: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Hide Tags Linked List Two Pointers thinking: (1)如果可以开设额外的空间,使用unordered_set存储遍历过的结点,出现重复时即为存在环形结构 (2)如果不适用额外的空间,及空间复杂度为O(1),这里使用快.

[LeetCode] Linked List Cycle II 单链表中的环之二

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参见我之前的一篇文章 (http://www.cnblogs.com/grandyang/p/4137187.html). 还是要设

LeetCode 141, 142. Linked List Cycle I+II

判断链表有没有环,用Floyd Cycle Detection算法,用两个快慢指针. class Solution { public: bool hasCycle(ListNode *head) { if (!head) return false; ListNode *slow, *fast; slow=fast=head; do{ if (fast==NULL || fast->next==NULL) return false; slow = slow->next; fast = fast-