C++ solution, 14ms


  • 0
    T

    class Solution {
    public:
    ListNode detectCycle(ListNode head) {
    if (!head) return NULL;
    ListNode
    slow = head;
    ListNode
    fast = head;
    while (fast && fast->next) {
    slow = slow->next;
    fast = fast->next->next;
    if (slow == fast) {
    fast = head;
    while (slow != fast) {
    slow = slow->next;
    fast = fast->next;
    }
    return slow;
    }
    }
    return NULL;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.