算法:判断链表是否有环

112 阅读1分钟
/**
 * 链表是否有环
 * @param head
 * @return
 */
public static boolean isCircle(ListNode head) {
    if (head == null || head.next == null) {
        return false;
    }

    ListNode slow = head;
    ListNode fast = head.next;

    while (slow != fast) {
        if (fast == null || fast.next == null) {
            return false;
        }
        slow = slow.next;
        fast = fast.next.next;
    }
    return true;
}
```
```