投机取巧的解法
/**
* 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) {
for(int i=0;i<10000;i++){
if(head==null){
return false;
}
if(Math.abs(head.val)>100000){
return true;
}
head.val=100000+1;
head=head.next;
}
return false;
}
}