程式語言 - LeetCode - C++ - 142. Linked List Cycle II



題目:


方法:

head -> (cycle start) -> (meeting point)
             |                |
             +----------------+

解答:

/**
 * 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) {
                ListNode* p1 = slow;
                ListNode* p2 = head;

                while (p1 != p2) {
                    p1 = p1->next;
                    p2 = p2->next;
                }

                return p1;
            }
        }

        return nullptr;
    }
};