tag:blogger.com,1999:blog-2682781734368178743.post5110644770020009899..comments2023-03-28T14:19:44.264-07:00Comments on Technical Interview Questions, Answers, and Tips: Linked List Loop/Cycle DetectionUnknownnoreply@blogger.comBlogger7125tag:blogger.com,1999:blog-2682781734368178743.post-31386608096367250482010-11-09T21:38:35.528-08:002010-11-09T21:38:35.528-08:00Having a flag in the structure is one way to solve...Having a flag in the structure is one way to solve it, but the interviewer will typically put the restriction that you cannot modify the structure. Otherwise, the problem is trivial.avid gardenerhttps://www.blogger.com/profile/08838915050477665426noreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-68115245549129765482010-11-08T20:43:00.887-08:002010-11-08T20:43:00.887-08:00instead of this why don't we have a flag in th...instead of this why don't we have a flag in the structure which represents if the node traversal is done or not.This makes job easy.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-23581165997721471052010-05-21T05:05:21.866-07:002010-05-21T05:05:21.866-07:00This logic covers all scenarios...
Good one !!!This logic covers all scenarios...<br />Good one !!!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-77321222321012111932010-03-06T16:00:47.645-08:002010-03-06T16:00:47.645-08:00Very good explanation and the code makes it very c...Very good explanation and the code makes it very clear for me<br /> .. but one comment in two places <br />After doing "fastPtr = fastPtr->next;" the next line must be "if(fastPtr == NULL) ..." if not then the line "if(fastPtr == slowPtr)" might give null pointer expecption.<br />redsowrdAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-9146839612898122532010-02-15T09:35:23.730-08:002010-02-15T09:35:23.730-08:00thanks for the answer, it is explaining....thanks for the answer, it is explaining....Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-59974843262466097402010-02-15T08:07:02.873-08:002010-02-15T08:07:02.873-08:00John, You are right that the loop will include the...John, You are right that the loop will include the intended tail node. But, by definition a tail node is one that has no next node (has a null next pointer). So, in the case we have a loop in the list, we do not have a tail node so to speak. <br />It is not enough to just traverse the linked list and see if it has a tail node or not, because a list with a loop will not have a tail node. If we Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2682781734368178743.post-79184923520062426492010-02-14T13:40:06.598-08:002010-02-14T13:40:06.598-08:00Because linked list has one next pointer points to...Because linked list has one next pointer points to the right of it (generally), the cycle must include the last (tail) node. Therefore there is no node whose next pointer is null. Therefore, again, I think it is enough to traverse the linked list and see whether there is a node whose next is null. <br /><br />Is the thought above correct or are there problems with it?Johnnoreply@blogger.com