Linked List Cycle

来源:互联网 发布:飞龙签名设计软件 编辑:程序博客网 时间:2024/05/04 06:06
//快慢指针

public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null)return false;
ListNode slow=head;
ListNode fast=head;
do{
if(fast==null||fast.next==null)return false;
fast=fast.next.next;
slow=slow.next;
} while(fast!=slow);
return true;
}
}
0 0