[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的速度是指针1的两倍,则有2(a+b) = a+b+c+b => a = c

         c. 因此,当两个指针在z点相遇之后,可以让指针1指向链表起点x,然后两个指针每次分别都走一步

             当两个指针再次相遇的时候,即为环的起点,即Y点

代码:

/**
 * 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* Solution::detectCycle(ListNode *head) {
    if (NULL == head) {
        return NULL;
    }
    ListNode *tmpHeadOne = head;
    ListNode *tmpHeadTwo = head;
    int step = 0;
    bool isCycle = false;
    while ((tmpHeadOne != NULL) && (tmpHeadTwo != NULL)) {
        if ((step != 0) && (tmpHeadOne == tmpHeadTwo)) {
            isCycle = true;
            break;
        }
        step++;
        tmpHeadOne = tmpHeadOne->next;
        tmpHeadTwo = tmpHeadTwo->next;
        if (tmpHeadTwo != NULL) {
            tmpHeadTwo = tmpHeadTwo->next;
        }
    }
    if (!isCycle) {
        return NULL;
    }
    tmpHeadOne = head;
    while (tmpHeadOne != tmpHeadTwo) {
        tmpHeadOne = tmpHeadOne->next;
        tmpHeadTwo = tmpHeadTwo->next;
    }
    return tmpHeadOne;
}
时间: 2024-09-19 10:03:32

[LeetCode 第11题] -- Linked List Cycle II的相关文章

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

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] 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

[LeetCode]82.Remove Duplicates from Sorted List II

[题目] Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2-

[LeetCode]81.Search in Rotated Sorted Array II

[题目] Search in Rotated Sorted Array II  Total Accepted: 3749 Total Submissions: 12937My Submissions Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a f

[LeetCode]80.Remove Duplicates from Sorted Array II

[题目] Remove Duplicates from Sorted Array II  Total Accepted: 4460 Total Submissions: 15040My Submissions Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your fun