题目描述
判断一个链表是否有环
题解
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null)
return false;
ListNode slow = head;
ListNode fast = head.next;
while(slow!=null&&fast!=null&&fast.next!=null)
{
if(slow==fast)
return true;
slow = slow.next;
fast = fast.next.next;
}
return false;
}
}