141. Linked List Cycle
5/1/2028 update: The solution below is the best so far. Because fast.next and fast.next.next have been used within the while loop. So the condition of while loop is fast != null and fast.next != null. If we call fast.next when fast == null, there will be null pointer issue. public class Solution { public boolean hasCycle ( ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null && fast . next != null) { fast = fast . next . next; slow = slow . next; if (fast == slow) return true; } return false; } } ------------------------------------------ ------------------------------------------ -------------------------------------- 思路: 一快一慢两个node, 快的走两步,慢的走一步。如果快的和慢的重合,那么这个linkedlist就有cycle。 实现: 先要检查head 和head.next是否为空。然后slow指向head, fast指向head.next。循环条件是fast != slow。在循环里面fast 和slow往下走。如果fast == null 或者fast.next == null则没有cycle。这里检查...