Skip to content

Instantly share code, notes, and snippets.

@JamesJi9277
Created April 9, 2015 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesJi9277/52356b141d58557635bd to your computer and use it in GitHub Desktop.
Save JamesJi9277/52356b141d58557635bd to your computer and use it in GitHub Desktop.
2.6
//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