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 is linear and non linear structure?

495


What stack means?

509


What is the Difference between treemap and hashmap?

494


Is sorting a math skill?

490


What type of memory allocation is referred for Linked lists?

810






Explain what are the notations used in evaluation of arithmetic expressions using prefix and postfix forms?

531


Are linked lists useful?

508


Construct a doubly linked list using a single pointer in each node?

547


Differentiate linear from non linear data structure?

544


Which sorting is worst?

592


Write a program to reverse a single linked list.

554


Define non-terminal nodes in a tree?

674


Explain the term tail recursion?

551


What is the minimum number of nodes in an avl tree of height h?

462


To describe the Complexity of Binary search, Quicksort and various other sorting and searching techniques..

564