day10

/*
https://leetcode-cn.com/problems/linked-list-cycle-lcci/
 */
package 链表;

public class _0208环路检测 {
    public ListNode detectCycle(ListNode head) {
    //利用快慢指针求解
        if(head==null || head.next==null) return null;
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast!=null && fast.next!=null){
            if (slow==fast) break;
            slow = slow.next;
            fast = fast.next.next;
        }
        return null;
    }
}

 

posted @ 2020-04-23 22:37  行之!  阅读(141)  评论(0编辑  收藏  举报