leetcode笔记: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?

二. 题目分析

题目的意思是给定一个链表的头指针,快速判断一个链表是不是有环,如果有环,返回环的起始位置。该题的经典做法是使用两个指针,两个指针均指向头结点,其中一个是快指针,一次走两步;另一个是慢指针,一次只走一步,当两个指针相遇时,证明有环。这种方法的时间复杂度为O(n),空间复杂度O(1),这里需要考虑一些特殊情况:

  • 空链表无环
  • 链表只有一个节点时可能构成自环

三. 示例代码

#include <iostream>

struct ListNode
{
    int value;
    ListNode* next;
    ListNode(int x) :value(x), next(NULL){}
};

class Solution
{
public:
    bool hasCycle(ListNode *head)
    {
        if (head == nullptr || head->next == nullptr)
            return false;
        ListNode* fast = head;
        ListNode* slow = head;
        while (fast->next->next)
        {
            fast = fast->next->next;
            slow = slow->next;
            if (fast == slow) return true;
        }
        return false;
    }
};

链表只有一个节点且该节点构成自环:

链表3->4->5->6->7,4->5->6->7形成环:

四. 小结

关于有环链表中快慢指针一定会相遇的解决方法,可以简单地证明:

如果有环的话,快慢指针都会进入有环的部分。

而一旦进入有环的部分,一快一慢,学过物理都知道,其实可以相当于一个静止另一个每次移动一格。

到此,为什么一定会相遇应该已经很明显了吧~

该方法广为人知,不知是否有更为精妙的解法?

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-13 10:32:57

leetcode笔记:Linked List Cycle的相关文章

【LeetCode】Linked List Cycle

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? 做完Linked List Cycle II在做这题简直就是阉割版.. fast每次前进两步,slow每次前进一步,如果相遇则为true,否则false class Solution { public: bool hasCycle(ListNo

Leetcode 142 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? 同Leetcode 141 Linked List Cycle 性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点 证明: 指

[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? 题意: 给定一个链表,找到环起始的位置.如果环不存在,返回NULL. 分析: (1)首先要判断该链表是否有环.如果没有环,那么返回NULL. (2)其次,当已知环存在后,寻找环起始的位置. 思路: (

【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? 思路:由[Leetcode]Linked List Cycle可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

LeetCode OJ - 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? 解题思路: 设置两个指针slow和fast,从head开始,slow一次一步,fast一次两步,如果fast能再次追上slow则有圈. 设slow走了n步,则fast走了2*n步,设圈长度m

LeetCode OJ - 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? 解题思路: 使用快慢指针,快指针每次走两步,慢指针每次走一步,若快指针能追上慢指针,则表明有圈. 代码如下: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNo

[C++]LeetCode: 73 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? 思路: 可以参考 LeetCode: 73 Linked List Cycle. 使用快慢两个指针,首先先判断链表是否存在环,如果存在,从相遇点开始,还有另外一个从链表头开始,同时移动一步,

【leetcode】Linked List Cycle (python)

题目分析见这里 class Solution: # @param head, a ListNode # @return a list node def detectCycle(self, head): if None == head or None == head.next: return None pfast = head pslow = head #找第一次相遇的点,若存在环路,则肯定会相遇 while pfast and pfast.next: pfast = pfast.next.nex

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][JavaScript]Linked List Cycle

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? https://leetcode.com/problems/linked-list-cycle/ 判断链表是否有环. 先是用了额外空间的做法. 移出每个访问过的节点,指向头节点,如果遇到了指向头节点的点,说明有环. 1 /** 2 * @par