How can one find a cycle in the linked list? IF found how
to recognize the cycle and delete that cycle?
Answer Posted / chethu
Why are you guys moving both the pointers one behind the other?
You can keep a pointer at the header and traverse the other and check if it comes back to header if it does then there is a cycle else there is no cycle..
bool find_cycle(Node* head){
Node* ptr1 = head;
Node* ptr2 = head->next;
while(ptr2 != NULL && ptr2->next != NULL)
{
if(ptr1 == ptr2){
printf("\nClycle present in thr LinkList\n");
return true;
}
ptr2 = ptr2->next->next;
}
return false;
}
This should be more efficient.
| Is This Answer Correct ? | 1 Yes | 1 No |
Post New Answer View All Answers
What is structure of data type?
How does insertion sort works?
What are the collision resolution methods?
Why merge sort is better than insertion sort?
What is Insertion sort, selection sort, bubble sort( basic differences among the functionality of the three sorts and not the exact algorithms)?
How do you determine if a binary tree is height balanced?
Define hash table?
Can hashmap have same key?
Define 2-3-4 tree?
What is adt in data structure with example?
What do you mean by open addressing?
Explain the expression trees?
Define the term “percolate down”?
Should I use hashmap or hashtable?
Is merge sort better than quick?