Goldman Sachs Interview Question
Developer Program Engineersmake to pointers..
increase the first one by 1 and the second one by 2 nodes..
where ever they are equal or fast pointer overtakes second one => loop detected.
while(fast->next==null)
{
if(fast == slow || fast->next == slow)
{//loop detected}
}
Traverse a linked list with two pointers .. one immediately followed by other one. if ter is a loop , the condition mentioned above is met..cool :)
add addresses of the node in hast table and every time check the table when new node is accessed if address value all ready exist then there is loop in the list
- rinku February 03, 2013/////============code
ptr=strat;
while(ptr!=NULL)
{
if(hash_has(ptr))
return "LOOP"
else
hash_insert(ptr);///insert into hash
ptr=ptr->nxt;
}
return "NO_LOOP"
=========================================