leetcode brush 34 title

Today another that the brush 141 is LeetCode question, circular linked list, the question here is not hard to solve directly speed pointer, in particular code is as follows:

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

 

Guess you like

Origin www.cnblogs.com/cquer-xjtuer-lys/p/11546065.html
Recommended