public static boolean hasCycle(ListNode head){
if(head==null||head.next==null)
return false;
ListNode slow=head;
ListNode fast=head.next;
while(slow!=fast)
{
if(fast==null||fast.next==null)
return false;
slow=slow.next;
fast=fast.next.next;
}
return true;
}
判断链表是否有环
最新推荐文章于 2021-05-11 18:15:13 发布