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
What is collection sort?
What is max heapify?
How do you sort elements in an arraylist?
Name some applications which use linked lists.
What’s the difference between enumeration and iterator interfaces?
What do you mean by balance factor of a node in avl tree?
State the difference between primitive and non-primitive data types?
Define balanced trees?
Which sorting algorithm is best for large data?
How to get largest and smallest number in an array?
Can arraylist contain duplicates?
Differentiate between compilers and interpreters.
What is 1d array?
What is data structure in programming language?
What is doubly linked list in data structure?