Cycle Detection
https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem
bool has_cycle(SinglyLinkedListNode* head) {
SinglyLinkedListNode* slow=head;
SinglyLinkedListNode* fast=head;
while(slow && fast && fast->next){
slow=slow->next;
fast=fast->next->next;
if(fast==slow){
return 1;
}
}
return 0;
}
Comments
Post a Comment