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
What is the difference between push and pop?
What is a linear search?
What is the best case for bubble sort?
Describe queue operation.
What is example of data?
How to initialize Dictionary using collection initialize?
Which sorting algorithm is best for large data?
Is sorting a math skill?
What does stack top do?
What is a string or array type?
Which is the parent class of deque
Which is better hashmap or treemap?
Complete structure of hashmap, very detail description, along with the basic coding of the hashmap internal implementation.
Define a Deque?
Does hashmap allow null keys?