Created
April 9, 2015 15:32
-
-
Save JamesJi9277/52356b141d58557635bd to your computer and use it in GitHub Desktop.
2.6
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
//Given a circular linked list, implement an algorithm which returns the node at the begining of the loop | |
public class Solution{ | |
public LinkedListNode findBegining(LinkedListNode head){ | |
LinkedListNode slow = head; | |
LinkedListNode fast = head; | |
while(fast != null || fast.next != null) | |
{ | |
slow = slow.next; | |
fast = fast.next.next; | |
if(fast == slow) | |
break; | |
} | |
slow = head; | |
while(slow != fast) | |
{ | |
slow = slow.next; | |
fast = fast.next; | |
} | |
return slow; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment