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
What is merge sort in daa?
How does a heap sort work?
What is the time complexity of hashmap get () and put () method?
Define general trees?
What is Another name of Dynamically allocating memory.
List the data structures which are used in hierarchical data model.
Which sorting algorithm has minimum number of swaps?
Differentiate between hashset and hashmap.
Explain quick sort?
Does treemap allow null keys?
Can arraylist store primitives?
What is binary search tree and explain its time complexity?
How helpful is abstract data type of data structures?
What do you mean by general trees?
What do you mean by overflow and underflow?