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


Please Help Members By Posting Answers For Below Questions

What are the applications of linked list?

476


what are the applications of Linked Lists?

508


What stack means?

499


What is a weighted graph?

547


Model a data structure for a DFA that takes an event as parameter and performs a desired action.

693






State the difference between stacks and linked lists?

512


What is a class user?

554


What is list data structure?

501


Is unordered_map a hash table?

460


How many types of arrays are there in visual basic?

443


How can I study data structures and algorithms?

507


What is binary tree example?

550


Why quicksort is faster?

481


How do you declare An array of three pointers to chars

502


What is a matrix? Explain its uses with an example

563