Wednesday, May 13, 2009

Find out a loop in a singly-linked list

1 comment:

  1. find_loop(Node *startnode)
    {
    Node *slow = *fast1 = *fast2 = startnode;
    while(slow){

    fast1 = fast2->next;
    fast2 = fast1->next;
    /* check if slow node matches with any */
    if (slow == fast1 || slow == fast2)
    return(TRUE);
    slow = slow->next;
    }
    return (FALSE);
    }




    }

    ReplyDelete