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

What is the -> in c?

1007


Is it cc or c in a letter?

1021


What are preprocessor directives in c?

1096


How many types of arrays are there in c?

1044


Can you assign a different address to an array tag?

1167


How can I write a function analogous to scanf?

1181


What are different types of operators?

1041


What are dangling pointers in c?

1225


what are the advantages of a macro over a function?

1141


Are bit fields portable?

1158


How many levels deep can include files be nested?

1139


What is a structure member in c?

997


What is extern keyword in c?

1129


Does sprintf put null character?

1039


Using which language Test cases are added in .ptu file of RTRT unit testing???

4276