LeetCode "Linked List Cycle II"

Nothing but admire: http://fisherlei.blogspot.com/2013/11/leetcode-linked-list-cycle-ii-solution.html

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode dum(0); dum.next = head;
        ListNode *sp = &dum;
        ListNode *fp = &dum;
        while (sp && fp)
        {
            sp = sp->next;
            fp = fp->next;
            if (fp) fp = fp->next;
            if (sp == fp) break;
        }
        if (!fp) return nullptr;

        sp = &dum;
        while (sp != fp)
        {
            sp = sp->next;
            fp = fp->next;
        }
        return fp;
    }
};
原文地址:https://www.cnblogs.com/tonix/p/3921781.html