Reprint Please specify source: Z_zhaojun's Blog
Original address: http://blog.csdn.net/u012975705/article/details/50412899
Title Address: https://leetcode.com/problems/linked-list-cycle-ii/
Linked List Cycle II
areturnthethebeginsreturnnullnottheitwithoutusingspace?
Solution (Java):
/** * Definition for singly-linked list. * Class ListNode {* int val; * ListNode Next; * ListNode (int x) {* val = x; * next = NULL; *} *} */ Public class solution { PublicListNodedetectcycle(ListNode head) {if(Head = =NULL|| Head.next = =NULL) {return NULL; } ListNode slow = head; ListNode fast = Head.next; while(Slow! =NULL&& Fast! =NULL&& Slow! = fast) {//if (slow = = fast) { //temp = slow; //break; // }slow = Slow.next; Fast = Fast.next = =NULL? Fast.next:fast.next.next; }if(slow = = fast) {slow = head; Fast = Fast.next; while(Slow! = fast) {slow = Slow.next; Fast = Fast.next; }returnSlow }return NULL; }}
leetcode:142. Linked List Cycle II (Java) solution