How can one find a cycle in the linked list? IF found how
to recognize the cycle and delete that cycle?
Answer Posted / kamran
As far as answer 2 is concerned..it is not correct because
ptr1 and ptr2 is both pointing towards Head. and below
while loop true at first attempt and both the pointer are
still pointing toward head so at first etration condition
will be true even though there is no loop :)
The modified verision is this
bool find_cycle(Node* head){
Node* ptr1 = head;
Node* ptr2 = head->next;
while(ptr1 != NULL && ptr2 != NULL && ptr2->next != NULL)
{
if(ptr1 == ptr2){
printf("\nClycle present in thr LinkList\n");
return true;
}
ptr1 = prt1->next;
ptr2 = ptr2->next->next;
}
return false;
}
Is This Answer Correct ? | 18 Yes | 1 No |
Post New Answer View All Answers
Why concurrenthashmap is fail safe?
Explain the Linked List
What is difference between hashmap and hashtable?
What are the two types of data?
If you do not initialize an array what will happen?
What is difference between list and array list?
Define balancing condition for AVL Tree.
Which is the parent class of sortedset
Does the minimal spanning tree of a graph give the shortest distance between any 2 specified nodes?
What is difference between rb tree and avl tree?
What is the best case complexity of bubble sort?
Explain about the different lists available in the collection?
Explain what is a spanning tree?
How to excel in data structures and algorithms?
How does a selection sort work?