Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions LinkedListCycle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct ListNode{
int data;
struct ListNode* next;
};
bool hasCycle(struct ListNode *head) {
if(head==NULL||head->next==NULL)
{
return false;
}
struct ListNode* ptr=(struct ListNode*)malloc(sizeof(struct ListNode*));
struct ListNode* ptrf=(struct ListNode*)malloc(sizeof(struct ListNode*));
ptrf=head;
ptr=head->next;
while(ptr!=NULL && ptr->next != NULL)
{
if(ptrf == ptr)
{
return true;
}
ptrf=ptrf->next;
ptr=ptr->next->next;
}
return false;
}
int main()
{
struct ListNode * head = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * secondNode = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * thirdNode = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * fourthNode = (struct ListNode*)malloc(sizeof(struct ListNode));

head->data = 3;
head->next = secondNode;

secondNode->data = 6;
secondNode->next = thirdNode;

thirdNode->data = 0;
thirdNode->next = fourthNode;

fourthNode->data = -2;
fourthNode->next = NULL;
bool h;
h = hasCycle(head);
printf("%d", h);
return 0;
}