My C++ solution


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

Log in to reply
 

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