Created
September 30, 2013 00:26
-
-
Save KodeSeeker/6757839 to your computer and use it in GitHub Desktop.
Find the starting node of a loop in a circularly linked list. **Important**
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Logical extension to hare-tortoise cycle detection algorithm | |
// first check for cycle using rabbit-tortoise algo then move back tortoise to head and advance both rabbit and | |
//tortoise one by one | |
// The point where they meet will be the start of the loop | |
/** | |
* Returns the node at the start of a loop in the given circular linked | |
* list. A circular list is one in which a node's next pointer points | |
* to an earlier node, so as to make a loop in the linked list. For | |
* instance: | |
* | |
* input: A -> B -> C -> D -> E -> C [the same C as earlier] | |
* output: C | |
* | |
* (CCI_0205) | |
* | |
* @param linkedList | |
* list to be tested | |
* @return the node at the start of the loop if there is a loop, null | |
* otherwise | |
*/ | |
public static IntegerNode findLoopStart(LinkedList linkedList) { | |
if (linkedList == null || linkedList.getHead() == null) { | |
return null; | |
} | |
IntegerNode loopStartNode = null; | |
IntegerNode slow = linkedList.getHead(); | |
IntegerNode fast = linkedList.getHead(); | |
while (slow != null && fast != null) { | |
slow = slow.getNext(); | |
if (fast.getNext() == null) { | |
loopStartNode = null; | |
break; | |
} | |
fast = fast.getNext().getNext(); | |
// If slow and fast point to the same node, it means that the | |
// linkedList contains a loop. | |
if (slow == fast) { | |
slow = linkedList.getHead(); | |
while (slow != fast) { | |
// Keep incrementing the two pointers until they both | |
// meet again. When this happens, both the pointers will | |
// point to the beginning of the loop | |
slow = slow.getNext(); // Can't be null, as we have a loop | |
fast = fast.getNext(); // Can't be null, as we have a loop | |
} | |
loopStartNode = slow;// can be fast as well!! | |
break; | |
} | |
} | |
return loopStartNode; | |
} |
Haha, happy to know it helped!
…On Thu, Jan 2, 2020 at 12:54 PM baghdad11 ***@***.***> wrote:
Well done i was trying so hard to find a solution to this ty so much now i
understand how it works by the way im only 16 who loves to code and learn
new stuff😅
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/6757839?email_source=notifications&email_token=AAK62MEOE4D6LJBAX7QL3VTQ3ZIBJA5CNFSM4KCGSGAKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF6ZHC#gistcomment-3125873>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAK62MHDKJ7HHS4ZBY445ALQ3ZIBJANCNFSM4KCGSGAA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well done i was trying so hard to find a solution to this ty so much now i understand how it works by the way im only 16 year old boy, who loves to code and learn new stuff😅