#include <stdio.h> #include <stdlib.h> struct linkedList{ int number; struct linkedList* next; }; typedef struct linkedList* NODE; int FindLoop(NODE *head) { NODE *fastPtr, *slowPtr; fastPtr = slowPtr = head; while(1) { if(!fastPtr || !fastPtr->next) return 0; else if(fastPtr == slowPtr || fastPtr ->next == slowPtr) return 1; // Loop Exist else{ slowPtr = slowPtr->next; fastPtr = fastPtr->next->next; } } return 0; }
I am a software developer. I am passionate about technology in general and distributed systems, Performance, Security in particular. I work mainly with open source software, specialising in Java and Unix-based operating systems.
Saturday, February 5, 2011
Find loop in a linked list
Subscribe to:
Posts (Atom)