Given a linked list, return the node where the cycle begins. If There is no cycle, return null .
Note:do not modify the linked list.
Follow up:
Can you solve it without using extra space?
141 The extension of the problem, to find out the cycle point.
It can be proved by mathematical method that the location of slow and find must be the point to be asked.
/*** Definition for singly-linked list. * Class ListNode {* int val; * ListNode Next; * ListNode (int x) {* val = x; * next = NULL; * } * } */ Public classSolution { PublicListNode detectcycle (ListNode head) {if(Head = =NULL|| Head.next = =NULL ) return NULL; ListNode Fast=Head; ListNode Slow=Head; while(Fast! =NULL&& Fast.next! =NULL) {Slow=Slow.next; Fast=Fast.next.next; if(Fast = =slow) {ListNode Find=Head; while(Find! =slow) {Find=Find.next; Slow=Slow.next; } returnfind; } } return NULL; }}
Leetcode 142. Linked List Cycle II-----java