public class Solution {
public boolean hasCycle(ListNode head) {
//若头节点为空,则无环
if(head == null) return false;
while(head.val != 100001 && head != null){
//依次将遍历过的节点标记
head.val = 100001;
head = head.next;
//若遍历到最后一个节点则无环
if(head == null) return false;
}
if(head == null) return false;
//退出循环原因是已经遍历到标记过的节点,说明有环
return true;
}
}