daily algorithm 判断链表是否有环

  public static boolean isLoopLink(ListNode head) {
        if (head == null) {
            return false;
        }
        ListNode fast = head.next;
        ListNode slow = head;
        while (fast.next != null) {
            if (fast == slow) {
                return true;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        return false;
    }

 

posted @ 2019-06-05 16:02  java渣渣  阅读(83)  评论(0编辑  收藏  举报