LeetCode linked-list-cycle linked-list-cycle-ii

linked-list-cycle

//解決方案:快慢指針,如果有環,則會相遇;使用hash存儲,需要額外申請空間

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
//方法1
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head == NULL)
            return false;
        ListNode *slow, *fast;
        slow = head;
        fast = head->next;
        if(fast == NULL)
            return false;
        while(slow != fast && fast != NULL)
        {
            slow = slow->next;
            fast = fast->next;
            if(fast != NULL) //important
                fast = fast->next;
            else
                return false;
        }
        if(slow == fast)
            return true;
        return false;
    }
};

//方法2
/*
class Solution {
public:
    bool hasCycle(ListNode *head) {
       unordered_map<ListNode*, bool> m;
        while(head)
        {
            if(m.find(head) != m.end())
                return true;
            m[head] = true;
            head = head -> next;
        }
        return false;
    }
};
*/

linked-list-cycle-ii

/*
解決方法:

一種方法是用之前的哈希表,第一次出現重複的那個結點即是環開始的地方。但是會需要額外O(n)的空間

但是,如何不申請額外空間,就找到環開始的地方呢?

看了別人的分析,在使用快慢指針時,可以找到兩指針的相遇點。然後再用兩個指針分別從鏈頭和相遇點出發,相同速度一步步推進,那麼兩指針將會在環開始的地方相遇。

a = (n-1)(b+c) + c

推導公式參考:[推到討論](https://www.nowcoder.com/questionTerminal/6e630519bf86480296d0f1c868d425ad)

*/
/**
 * 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) {
        if(head == NULL)
            return NULL;
        if(head->next == NULL)
            return NULL;
        if(head->next == head)
            return head;
        unordered_map<ListNode*, bool> m;
        static ListNode *res = NULL;
        while(head != NULL)
        {
            if(m.find(head) != m.end())
            {
                res = head;
                return res;
            }
            m[head] = true;
            head = head->next;
        }
        return NULL;
    }
};

//方法2
/*
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        static ListNode *res = head;
        if(head == NULL)
            return NULL;
        ListNode *fast, *slow;
        fast = slow = head;
        //鏈表較短的時候,情況有限,可以直接處理(優化)
        if(fast->next == NULL)
            return NULL;
        do
        {
            slow = slow->next;
            fast = fast->next;
            if(fast != NULL)
                fast = fast->next;
            else
                return NULL;
        }while(slow != fast && fast != NULL);
       if(fast == slow)
       {
            while(head != slow)
            {
                head = head->next;
                slow = slow->next;
            }
            res = head;
            return res;
       }
       else
           return NULL;
    }
};
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章