How can one find a cycle in the linked list? IF found how
to recognize the cycle and delete that cycle?

Answer Posted / kedar

Ans #6 is the correct version.

//ensure that it not a empty list or list with single node
or list with 2 nodes which is not a cycle.

bool find_cycle(Node* head){

if(head == NULL // empty list
|| head->next == NULL // list with 1 node
|| head->next>next == NULL) // 2 nodes w/o cycle
{
return false;
}

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 ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Who invented data structure?

639


What is a undirected graph?

571


how to delete first node from singly linked list?

637


What is storage structure in data structure?

513


How to use appendNode() in linkedlist()?

663






What is int data type?

547


Define graph?

731


What is data structure? Explain.

539


What are the differences between b tree and b+ tree?

494


Why is treeset sorted?

525


Can treeset contain null?

505


How do you sort a list in reverse order?

476


What are control structures?

537


Define a complete binary tree?

541


What are the advantages of modularity?

509