1. public class Node{
    2. public int val;
    3. public Node next;
    4. }
    5. public boolean isHasLoop(Node head){
    6. if(head == null) return false;
    7. Node slow = head.next;
    8. if(slow == null) return false;
    9. Node fast = slow.next;
    10. while(fast != null && slow != null){
    11. if(fast == slow) return true;
    12. slow = slow.next;
    13. fast = fast.next;
    14. //快指针走两步时,要注意避免空指针异常
    15. if(fast != null) fast = fast.next;
    16. }
    17. return false;
    18. }