//链表是否有环
//走三步,走一步 很慢才能相遇 ,或者永远不相遇
public boolean hasCycle() {
Node fast = this.head;
Node slow = this.head;
while (fast != null && fast.next != null) {
//可以快速相遇
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
// return true;
break;
}
}
if (fast == null || fast.next == null) {//说明环为空
return false;
}
return true;
}
链表是否有环
最新推荐文章于 2021-05-08 21:18:18 发布