题解:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
//空链表、单节点链表一定不会有坏
while (fast != null &&fast.next != null){
fast = fast.next.next;//快指针,一次移动两步
slow = slow.next; //慢指针,一次移动一步
if (fast == slow){ //快慢指针相遇,表明有环
return true;
}
}
return false; //正常走到链表末尾,表面没有坏
}
}