Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 28-08-2011, 12:37 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Aug 2011
Posts: 1
Default detecting a loop in a linked list


hey can someone tell me how to find a loop in a linked list....ive searched a lot on google.i found the famous floyed`s algorithm but what i dont understand is that how can this algorithm if the length of the loop is bigger than 4 node...as in what if a have a linked list that goes on like
a->b->c->d->e->f->g->h->i->j->k->l->m->b.....


could someone please suggest an algorithm for this or if floyd s works for this then how?
zorrotech2010 is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 28-08-2011, 01:16 PM   #2 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: detecting a loop in a linked list

you may try this-
introduce a counter variable in the basic element structure and initialize it to 0. starting from 'a', start iterating through the elements. before moving on to the next element, increment the counter variable by 1 and then check its value, if value>1, there exists a loop as this element has been encountered before, if value <= 1, then move on to the next element.

you may also achieve it by defining a 2-d array which stores the memory address of elements and their corresponding encounters during iteration.
nims11 is online now   Reply With Quote
Old 29-08-2011, 02:09 PM   #3 (permalink)
gkbhat.blogspot.com
 
Join Date: Apr 2008
Location: Mangalore/Bangalore
Posts: 103
Default Re: detecting a loop in a linked list

Floyds alogorithm(Tortoise and hare algorithm) should suffice your case also. When there is a loop the hare is stuck in the loop however fast it runs and the tortoise will eventually catch up. For a very good discussion see here Interview: Remove Loop in linked list - Java - Stack Overflow
__________________
blogging at http://gkbhat.blogspot.com
gk2k is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:34 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2