leetcode笔记:Linked List Cycle 2

一. 题目描述

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?

二. 题目分析

在Linked List Cycle题目中,使用了两个指针fast与slow检查链表是否有环,该题在此基础上,要求给出链表中环的入口位置,同样需要注意空间复杂度。为了方便解释,以下给出一个有环链表:

其中,设链表头节点到环入口处的距离为X,环长度为Y,使用fastslow两个指针,fast指针一次前进两步,slow指针一次前进一步,则他们最终会在环中的K节点处相遇,设此时fast指针已经在环中走过了m圈,slow指针在环中走过n圈,则有:

fast所走的路程:2*t = X+m*Y+K

slow所走的路程:t = X+n*Y+K

由这两个方程可得到:

2X + 2n*Y + 2K = X + m*Y + K

进一步得到: X+K = (m-2n)Y

可得到一个结论,即当fastslow相遇时,可使用第三个指针ListNode* cycleStart = head;,从链表头节点往前走,slow指针也继续往前走,直到slowcycleStart相遇,该位置就是链表中环的入口处。

三. 示例代码

#include <iostream>
using namespace std;

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

class Solution
{
public:
    ListNode *detectCycle(ListNode *head)
    {
        if (head == NULL || head->next == NULL || head->next->next == NULL)
            return head;
        ListNode* fast = head;
        ListNode* slow = head;
        while (fast->next->next)
        {
            fast = fast->next->next;
            slow = slow->next;
            if (fast == slow)
            {
                ListNode* cycleStart = head;
                while (slow != cycleStart)
                {
                    slow = slow->next;
                    cycleStart = cycleStart->next;
                }
                return cycleStart;
            }
        }
        return NULL;
    }
};

一个有环链表,环从第二个节点开始:

四. 小结

解答与链表有关的题目时,要多画图,找规律,否则容易遇到各种边界问题。

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

时间: 2024-08-24 03:17:48

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

【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