Last active
October 19, 2015 09:52
-
-
Save KevinKu/43da87efaae848692b43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* struct ListNode *next; | |
* }; | |
*/ | |
struct ListNode *detectCycle(struct ListNode *head) { | |
static struct ListNode *Max_cycle_size_index = NULL; | |
static struct ListNode *list_begin = NULL ; | |
static struct ListNode *rabbit = NULL; | |
if( list_begin == NULL ) | |
{ | |
rabbit = head; | |
list_begin = head; | |
} | |
if( Max_cycle_size_index == NULL ) | |
{ | |
if( rabbit && rabbit->next && rabbit->next->next ) | |
{ | |
head = head->next; | |
rabbit = rabbit->next->next; | |
if( head == rabbit ) | |
{ | |
Max_cycle_size_index = rabbit ; | |
return detectCycle( list_begin ); | |
} | |
return detectCycle( head ); | |
} | |
Max_cycle_size_index = NULL; | |
list_begin = NULL; | |
rabbit = NULL; | |
return NULL; | |
} | |
else | |
{ | |
if( rabbit == head ) | |
{ | |
Max_cycle_size_index = NULL; | |
list_begin = NULL; | |
rabbit = NULL; | |
return head; | |
} | |
head = head->next; | |
rabbit = rabbit->next; | |
return detectCycle( head ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment