Linked List: Detect a Cycle in a linked list – List Reversal
Posted by tekpool on September 29, 2006
Q: Find whether or not there is a cylcle in a linked list? If so, Fix the cycle.
There are several solutions to this problem. I already discussed a few soIutions prior to this. Lets look into an easy but not so commonly used solution.
Sol 5: Find Cycle through List Reversal
We’ve looked into some classic approaches of Linked List Cycle Finding Methods (Hare and Tortoise) in the previous posts. Lets see a more easier straightforward approach. I am a bit surprised this is not a usually used method.
The idea is pretty simple. Reverse the list!!. If you meet the head in the process of reversal, you’ve got a cycle. Below is some sample code for this. The exit condition, when you do not find a cycle is when you reach the tail node. Either ways the list needs to be reveresed again, so that we leave the list in its initial state.
void LinkedList::IsLooped1() {
LNode* prev=NULL;
LNode* current=NULL;
LNode* tmp=head;
while(tmp) {
prev=current;
current=tmp;
tmp=tmp->next;
if(tmp==head)
cout < < "Found Loopn";
current->next=prev;
}
head=current;
Reverse(); //Leave list in original state
}
Sharad Upadhyay said
if(tmp==head)
cout
Sharad Upadhyay said
Just thinking how (tmp==head) will work? It’ll work for circular link list. Not for smaller loops (will it go in infinite loop?)
Tekpooler said
No it will not..
There are 2 cases here
1- There is a cycle (Circular List is just a special case)
2- There is no cycle
If you are reversing the list and there is no cycle you reach a node that is NULL (end of the list) and then you terminate..
However if there is a cycle you WILL reach the head pointer in the process.. Take a simple list with a cycle and manually run throught the reversal process to see how it works.. pretty straightforward..
Inder said
Moreover the algo..should be generic enough to take any node pointer in the list and figure out whether there is a loop or not. It need be dependent on the list head, as by the very definition of circular list(special case of the loop) there is no head node.
bhaskar said
(tmp==head) will not work at all.
surekha said
I am not clear with the explanation, as m a beginner to industry life , can you be bit more clear..
BSG said
take two pointers
set one pointer1 to point any node in the list
set pointer2 to point pointer->next
then
while(pointer2)
{
pointer2=pointer2->next;
if(pointer1==pointer2)
{
printf(“circular list”);
break;
}
}
if the list is circular message is printed otherwise
when pointer2 reachs the tail and while loop ends
$anchor$basketball Betting,final Four,final Four Betting,final Four Gambling,final Four Sports Book,final Four Sportsbook,march Madness,march Madness Betting,march Madness Gambling,march Madness Sports Book,march Madness Sportsbook,ncaa,ncaa Betting,ncaa said
$anchor$basketball Betting,final Four,final Four Betting,final Four Gambling,final Four Sports Book,final Four Sportsbook,march Madness,march Madness Betting,march Madness Gambling,march Madness Sports Book,march Madness Sportsbook,ncaa,ncaa Betting,…
$anchor$basketball Betting,final Four,final Four Betting,final Four Gambling,final Four Sports Book,final Four Sportsbook,march Madness,march Madness Betting,march Madness Gambling,march Madness Sports Book,march Madness Sportsbook,ncaa,ncaa Betting,nc…
Bragaadeesh said
Comprehsive solution for reversing a Singly Linked List can be found here with illustrative pictures and complete working code.
http://technicalypto.blogspot.com/2010/01/java-program-to-reverse-singly-linked.html
hunter said
the point that could be easily overlooked is at the entry point of the smaller loop. Normal traversing will end up in an endless loop in it, but reversing won’t, on the contrary it will lead the process back to the head of the linked list.
The algorithm works.
Treuhaender Verzeichnis said
Treuhaender Verzeichnis…
Linked List: Detect a Cycle in a linked list – List Reversal « Technical Interview Questions…
Dave L. said
Your solution only works for a linked list that is an entire cycle, not a linked list with a loop somewhere in it. The typical interview question is that the loop exists somewhere but doesn’t necessarily include the head node. To use your O(n) solution here, you’d basically have to run it on each node, turning it into O(n-squared) solution.
Ubiquinol Benefits said
Ubiquinol Benefits…
[...]Linked List: Detect a Cycle in a linked list – List Reversal « Technical Interview Questions[...]…
cheapest xboa games said
cheapest xboa games…
[...]Linked List: Detect a Cycle in a linked list – List Reversal « Technical Interview Questions[...]…