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 dynamic variable in c?

569


What is the difference between struct and typedef struct in c?

661


What is the difference between int main and void main?

578


What will be your course of action for a push operation?

669


Why functions are used in c?

592






Do array subscripts always start with zero?

788


write a program to reverse a every alternetive words in a string in a place. EX: Input is "this is the line of text" Output should be "shit is eht line fo text" Please any one tell me code for that.

1580


Explain logical errors? Compare with syntax errors.

633


Explain what is a stream?

613


Explain the term printf() and scanf() used in c language?

600


how can use subset in c program and give more example

1507


Explain what are run-time errors?

612


Which is the best website to learn c programming?

585


What is the size of array float a(10)?

659


When should a type cast not be used?

627