判断单链表有没有环
方法一: 快慢指针
private static boolean hasCycle(Node head){
Node fast = head;
Node slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
return true;
}
}
return false;
}
blog comments powered by Disqus