LeetCode——Linked List Cycle II

若同时从一个环的某个点出发,fast指针每次走两步,slow指针每次走一步,则可证明slow指针走回起点时,fast指针也恰好到达起点。两种思路:一、fast指针每次比slow指针多走一步,fast指针想与slow指针同时到达某点,必须超过slow指针一圈,每次多走一步,假设环的节点数为N,则多走一圈需要走N次,此时slow指针恰好走完一圈指向出发点,fast指针亦然;二、fast指针的速度是slow的两倍,slow走k步,fast就走了2k步,fast指针要超过slow指针一圈即N步,即2k-k=N,一个很简单的方程,得k=N,所以slow指针走N步回到出发点时,fast和slow指针再次指向同一点。

那该怎么求解本题呢?设slow指针到达环的起始点要走k步,则fast指针此时已走了2k步,按照前述,fast指针想“赶上”slow指针需要走N-k次,此时fast和slow指针位于距环的起始点N-k步的位置,但N-k是指从起始点到这个点的步数,我们不能往回走,只能往前走,所有想再次回到起始点,必须再走N-(N-k)即k步。于是思路就很清晰了:先找出fast和slow指针“重合”即相等的地方,此时令slow = head,slow和fast每次只走一步,当走了k步(这个值不重要,只须判断fast和slow是否相等即可)fast和slow指针指向同一个节点使,这个节点即是环的起始点。若不存在这样的点,返回空指针。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {

        ListNode *slow = head;
        ListNode *fast = head;
        while(fast && fast->next)
	{
	    slow = slow->next;
            fast = fast->next->next;

            if (slow == fast){
                slow = head;
                while (slow != fast)
		{
                    slow = slow->next;
                    fast = fast->next;
                }
                return slow;
            }
        }

        return NULL;
    }
};

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

时间: 2024-11-06 22:53:24

LeetCode——Linked List Cycle II的相关文章

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

LeetCode: Linked List Cycle II [142]

[题目] 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? [题意] 给定一个单向链表,如果链表有环,则返回环开始的位置. [思路] 仍然是维护两个指针, p1, p2, p1每次走一步, p2每次走两步 假设进入环之前要走X步,环长为y步,p2第

[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 ii 解题报告

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? SOLUTION 1: 最开始的想法和 Linked List Cycle这一题一样. SOLUTION 2: 同样是两个指针,一快一慢,相遇时跳出循环,只

[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.

[LeetCode] Linked List Cycle II, Solution

Question : 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? Anaylsis : 首先,比较直观的是,先使用Linked List Cycle I的办法,判断是否有cycle.如果有,则从头遍历节点,对于每一个节点,查询是否在环里面,是

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? 题目意思很简单,判断链表是否有环,若有,返回环的位置. 一开始,用了原来I的方法,结果会出现超时. public ListNode detectCycle(ListNode head) { boole

[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] 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? Hide Tags Linked List Two Pointers 开始犯二了一点,使用了node 的val 作为判断,其实是根据内存判断.找出链表环的起始位置,这个画一下慢慢找下规律便可以: 思路

leetcode Linked List Cycle II python

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ if n