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

解题思路

设链表长度为n,头结点与循环节点之间的长度为k。定义两个指针slow和fast,slow每次走一步,fast每次走两步。当两个指针相遇时,有:

  • fast = slow * 2
  • fast - slow = (n - k)的倍数
    由上述两个式子可以得到slow为(n-k)的倍数

两个指针相遇后,slow指针回到头结点的位置,fast指针保持在相遇的节点。此时它们距离循环节点的距离都为k,然后以步长为1遍历链表,再次相遇点即为循环节点的位置。

实现代码

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

 //Runtime:16 ms
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if (head == NULL)
        {
            return NULL;
        }

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

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

            return slow;
        }

        return NULL;
    }
};
时间: 2025-01-02 03:56:38

[LeetCode] Linked List Cycle II的相关文章

[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? 分析: 首先使用快慢指针技巧,如果fast指针和slow指针相遇,则说明链表存在环路.当fast与slow相遇时,slow肯定没有遍历完链表,而fast已经在环内循环了n圈了(1<=n)设s

[LeetCode 第11题] -- Linked List Cycle II

题目链接: Linked List Cycle II 题目意思: 给定一个链表,如果链表有环求出环的起点,否则返回NULL 解题思路:      1. 判断链表是否有环: 两个指针,一个一次走一步,一个一次走两步,如果指针相遇说明有环,否则无环.     2. 如果有环的情况下,我们可以画个图(图片来自网络)                   假设两个指针在z点相遇.则          a. 指针1走过路程为a + b:指针2走过的路程为 a+b+c+b          b. 因为指针2的

[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? 解题思路 使用快慢两个指针,如果快的指针赶上了慢的,则说明存在回路. 实现代码 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNod

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? 本来不是很难的一题,提交了n遍,一直运行时错误,为什么呢?因为一个指针判断的问题,由于要向后移动两个指针,因为p->next也需要判断,而不能仅仅判断p,不然p=p->next->next会有

[LeetCode]141.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? [题意] 给定一个链表,确定它是否包含一个环. [分析] 最容易想到的方法是,用一个哈希表 unordered_map<int, bool> visited,记录每个元素是否被访问过,一旦出现某个元素被重复访问,说明存在环. 空间复杂度 O(n),时间复杂度 O(N

[LeetCode 第10题] -- Linked List Cycle

题目链接: linked List Cycle 题目意思: 给定一个链表,判断链表是否有环 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head); }; b

[LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output:

[LeetCode]40.Combination Sum II

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

[LeetCode]113.Path Sum II

[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [分析] 题目要求求出所有路径