How can one find a cycle in the linked list? IF found how
to recognize the cycle and delete that cycle?
Answer Posted / chethu
Why are you guys moving both the pointers one behind the other?
You can keep a pointer at the header and traverse the other and check if it comes back to header if it does then there is a cycle else there is no cycle..
bool find_cycle(Node* head){
Node* ptr1 = head;
Node* ptr2 = head->next;
while(ptr2 != NULL && ptr2->next != NULL)
{
if(ptr1 == ptr2){
printf("\nClycle present in thr LinkList\n");
return true;
}
ptr2 = ptr2->next->next;
}
return false;
}
This should be more efficient.
| Is This Answer Correct ? | 1 Yes | 1 No |
Post New Answer View All Answers
How would you reverse the characters of an array?
What is sorting in data structure?
Explain implementation of deletion from a binary tree.
What is a map programming?
List out a few of the applications that make use of Multilinked Structures?
Is list an array?
Define double linked list?
List the abstract operations in the set?
Name few classes that implement collection interface?
What are the different binary tree traversal techniques?
What is a minimum spanning tree?
What is the difference between adt and data structure?
Can a null element added to a treeset or hashset?
What is the difference between null and void pointer in data structures?
Differentiate between priorityqueue and treeset.