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 would you find a cycle in a linked list?

Answer Posted / jaroosh

The above method is working of course, but is not the most
efficient. Other methods are however quite complex and not
so easy to explain.
Anyway, to exemplify this aforementioned method, maybe not
the most efficient code, but off the top of my head, hope
there are no misspellings.

bool isCyclic(LinkedNode *list)
{
if(list == NULL || list->next == NULL) return false;
LinkedNode *node1 = list, *node2 = node1->next;
while(node1 != node2)
{
if(node1==NULL || node2==NULL || node2->next == NULL)
return false;
node1 = node1->next;
node2= node2->next->next;
}
return true;
}

NOTE: the assumption is that for noncyclic list, the last
node has next pointer set to NULL.

Is This Answer Correct ?    6 Yes 7 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

When would you use a pointer to a function?

1010


What is c system32 taskhostw exe?

989


Explain what are its uses in c programming?

1036


Why do we use & in c?

958


What are the 5 elements of structure?

1050


What is use of bit field?

1254


write a program for the normal snake games find in most of the mobiles.

2225


Why is main function so important?

1064


What is a example of a variable?

967


What is build process in c?

1097


What is a pointer in c?

1458


What is getch () for?

1166


write a program to create a sparse matrix using dynamic memory allocation.

4857


Describe the difference between = and == symbols in c programming?

1316


Why clrscr is used in c?

964