判断链表是否有环
知识点:链表 快慢指针
描述
输入一个长度为n链表,反转链表后,输出新链表的表头。
public class Solution {
public boolean hasCycle(ListNode head) {
if(haed==null){
return false;
}
ListNode slow=head;
ListNdde fast=head;
while(fast!=null && fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(fast==slow)
return true;
}
return false;
}
}