非独立思考

    1. public static boolean hasCycle(ListNode head) {
    2. // 快慢指针
    3. if (head == null || head.next == null) {
    4. return false;
    5. }
    6. ListNode slow = head;
    7. ListNode fast = head.next;
    8. while (fast != slow) {
    9. // 个人觉得这里是否需要同时判断fast与fast.next都为null是需要看题意的
    10. // 我觉得这里是因为,fast的变化是2跳。而且fast==null先判定也是防止fast.next空指针异常
    11. if (fast == null || fast.next == null) {
    12. return false;
    13. }
    14. slow = slow.next;
    15. fast = fast.next.next;
    16. }
    17. return true;
    18. }