标签:style blog color io div sp log on c
public class Solution { public ListNode detectCycle(ListNode head) { ListNode slow = head; ListNode fast = head; boolean hasCycle = false; while (fast != null) { slow = slow.next; if (fast.next == null) break; else fast = fast.next.next; if (fast == slow) { hasCycle = true; break; } } if (!hasCycle) return null; slow = head; while(slow != fast) { slow = slow.next; fast = fast.next; } return slow; } }
[LeetCode] Linked List Cycle II
标签:style blog color io div sp log on c
原文地址:http://www.cnblogs.com/yuhaos/p/3961581.html