Skip to content

Instantly share code, notes, and snippets.

@hilda8519
Created June 27, 2014 04:53
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 hilda8519/2a4317a8d9fa1a9b8056 to your computer and use it in GitHub Desktop.
Save hilda8519/2a4317a8d9fa1a9b8056 to your computer and use it in GitHub Desktop.
import java.awt.List;
import java.util.*;
public class findBenning {
public static ListNode findBeginning(ListNode head){
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast){
break;
}
}
if(fast==null|| fast.next==null){
return null;
}
slow=head;
while(slow!=fast){
slow=slow.next;
fast=fast.next;
}
return fast;
}
class ListNode{
public Object data;
int val;
ListNode next;
ListNode(int i){
val=i;
next=null;
}
}
public static void main(String[] args) {
//{1-2-3-4-5-3}
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);
head.next.next.next.next.next = head.next.next;
//expect 3
System.out.println(findBeginning(head));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment