Country: United States

Comment hidden because of low score. Click to expand.
4
of 4 vote

check this ..

Comment hidden because of low score. Click to expand.
3
of 3 vote

Use Floyd's Cycle Finding Algorithm

``````int isListCircular(ListNode* head){
return 0;
while(fast && fast->next){
if(fast->next->next==slow)
return 1;
fast=fast->next->next;
slow=slow->next;
}
return 0;
}``````

Comment hidden because of low score. Click to expand.
0

I've also heard this algo described as "chasing pointers".

Comment hidden because of low score. Click to expand.
0

Yes this is also called Tortoise and Hair algorithm.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````#include<stdio.h>

typedef struct node_s {
void *data;
struct node_s *next;
} NODE;

int list_has_cycle(NODE *list)
{
NODE *fast=list;
while(1) {
if(!(fast=fast->next)) return 0;
if(fast==list) return 1;
if(!(fast=fast->next)) return 0;
if(fast==list) return 1;
list=list->next;
}
return 0;
}

int main()
{
NODE n1, n2, n3, n4, n5;

n1.next=&n2;
n2.next=&n3;
n3.next=&n4;
n4.next=&n5;
n5.next=NULL;

printf("Test without cycle: ");
if(list_has_cycle(&n1)) printf("cycle\n");
else printf("no cycle\n");

n5.next=&n3;

printf("Test with cycle: ");
if(list_has_cycle(&n1)) printf("cycle\n");
else printf("no cycle\n");

return 0;``````

}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.