Skip to content

Instantly share code, notes, and snippets.

@KevinKu
Last active October 19, 2015 09:52
Show Gist options
  • Save KevinKu/43da87efaae848692b43 to your computer and use it in GitHub Desktop.
Save KevinKu/43da87efaae848692b43 to your computer and use it in GitHub Desktop.
/**
* 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