Jump to content
ChetanaSforum
Sign in to follow this  
contactme.rajesh

How to find loop in a linked list if exists

Recommended Posts

hi all, we are given a linked list. How to find if there exists a loop or not in linked list. If exists then how many loops....Could anyone provide me the solution...Thanks in advance...

Share this post


Link to post
Share on other sites

 

:) Loop means what please describe it clearly.See if u wanna check that its a circular link list that what u have to do that u have to store each and every node address in a list and on traverse u have to check that the address is present in that list or not.If it is present that is repetative than its a circular link.. other wise if its a single link list than it must end to null..

Share this post


Link to post
Share on other sites

accor t one friend gvn that we hve t store the addr & repaeating loop & whenever we find the rep addrthen loop is present... t solve this involves fraught with periil.. rather than tht llr t that use some pointer(s) pointing t any element in the list.. and with the help of another pointer(y) fwd it . so whnvr y->next=s then we say that loop is presence naveen, :) iv b.tech :)

Share this post


Link to post
Share on other sites

accor t one friend gvn that we hve t store the addr & repaeating loop & whenever we find the rep addrthen loop is present... t solve this involves fraught with periil.. rather than tht llr t that use some pointer(s) pointing t any element in the list.. and with the help of another pointer(y) fwd it . so whnvr y->next=s then we say that loop is presence naveen, :) iv b.tech :)

Firstly, i want to request you of not using your chat language. Its very hard to understand what you are trying to say....and it makes your post invaluable. So, please take care of that in your posts. Now coming to the point, easiest way to handle the problem is to add a new data member say a character, which will act as a flag or indicator. Initially you have a 'F' in it, in all nodes. When you traverse your list, make it 'T'. Check that before getting node->next==NULL, if you get a T value, it means your list is circular.Cheers....

Share this post


Link to post
Share on other sites

Firstly, i want to request you of not using your chat language. Its very hard to understand what you are trying to say....and it makes your post invaluable. So, please take care of that in your posts. Now coming to the point, easiest way to handle the problem is to add a new data member say a character, which will act as a flag or indicator. Initially you have a 'F' in it, in all nodes. When you traverse your list, make it 'T'. Check that before getting node->next==NULL, if you get a T value, it means your list is circular.Cheers....

i have a way Create two pointers, each set to the start of the list.Update each as follows:while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2)pointer2=pointer2->next; if (pointer1 == pointer2) { print (\"circular\n\"); }}If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it?s either 1 or 2 jumpsuntil they meet.chk n reply..millan :rolleyes: :rolleyes:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  



×