/Linked List LOOP

  Node*floyeddetectLoop(Node*head){

            if(head==NULL)

            return NULL;

            

            Node*slow=head;

            Node*fast=head;

            while(slow!=NULL && fast!=NULL){

                fast=fast->next;

                if(fast!=NULL){

                    fast=fast->next;

                }

                slow=slow->next;

                if(slow==fast){

                    return slow;

                }

                

            

            return NULL;

        }

        // code here

        Node*getStartingNode(Node*head){

            if(head==NULL)

            return NULL:

            

            Node*intersection=floyeddetectLoop(head);

            Node*slow=head;

            while(slow!=intersection){

                slow=slow->next;

                intersection=intersection->next;

            }

            return slow;

        }

        

    



        void remove_Loop(Node*head){

            if(head==NULL)

            return;

            Node*startOfLoop=getStartingNode(head);

            Node*temp=startOfLoop;

            while(temp->next !=startOfLoop){

                temp=temp->next;

            }

            temp->next=NULL;

        }

No comments:

Post a Comment

/Linked List LOOP

  Node*floyeddetectLoop(Node*head){             if(head==NULL)             return NULL;                          Node*slow=head;            ...