要点: 

1.  出了第一个while loop后,要先判断fast指针的情况。

 1 public class Solution {
 2     public ListNode detectCycle(ListNode head) {
 3         if (head == null) {
 4          return head; 
 5         }
 6         ListNode fast = head;
 7         ListNode slow = head;
 8         while (fast != null && fast.next != null) {
 9             fast = fast.next.next;
10             slow = slow.next;
11               if (slow == fast) {
12                 break; 
13               }
14         }
15     
16         if (fast == null || fast.next == null) {
17           return null;
18         }
19         
20         while (head != slow) {
21          head = head.next;
22          slow = slow.next;
23         }
24 
25         return head;
26     }
27 }

 

posted on 2018-03-02 07:25  mayinmiao  阅读(92)  评论(0编辑  收藏  举报