Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What is definition list?

990


How do you access the values within an array?

1119


Who created quicksort?

1024


If you are given a choice to use either arraylist and linkedlist, which one would you use and why?

1044


Can arraylist contain duplicates?

915


What is faster array or arraylist?

955


What is comparable interface?

1007


What do you mean by overflow and underflow?

1012


What is raid (redundant array of inexpensive disks)?

1038


Write an algorithm to find middle element in the linked list.

962


What are the advantages and disadvantages of linked list?

947


What are the objectives of studying data structures?

1187


Which is the parent class of enumset class?

1019


What are the issues that hamper the efficiency in sorting a file?

1066


What's the difference between a hashtable and a hashmap?

973