@ above
well this a ver old and easy problem.. but unlike u, criticizing others wen
u knw the solution i wud rather post my solution..
int removeCycle(list head)
{
struct listnode * slow, *fast,*slow;
slow=fast=head;
do
{
if(!fast || ! fast->next) rteurn -1;
slow=slow->next;
fast=fast->next->next;
} while(slow!=fast);
slow1=head;
while( slow1!=slow)
{
prev=slow;
slow=slow->next;
slow1=slow1->next;
}
prev->next=null;
return 1;
}
well this is the code for solving ur prob.. but i hv nt attached y this
works.. i hope u ll work out on this.. rather than having spoon feeding..
still if u cant work out, u can ask it.. i would love to answer and explain
to a genius (/ or the person who considers himself genius and the one who
considers others doubts as homework problem and his own doubts as a big
tricky problem although it is too old and common problem :P).........
On Wed, Dec 22, 2010 at 9:11 PM, Saurabh Koar <[email protected]>wrote:
> Finding whether a loop exists or not in a linked list, is a very
> familiar problem.But I want an algorithm that will find the node that
> is causing the loop.
> Well,I have an approach.Start from the head.Copy its data into an
> array.Mark node's data as infinity.Move to the next node.When u find
> node->next->data=infinity u will say that the current node is causing
> the loop.Then restore the data of the linked list from the array.But I
> think more optimized algorithm is possible.Reply if you know more
> optimized way.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.