public class LinkCircleChecker {
public static class Node{
//直观起见,设置为public
public int value;
public Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
public static Boolean checkCircle(Node head){
Node fast = head;
Node slow = head;
while (fast!=null&&fast.next!=null) {
slow= slow.next;
fast=fast.next.next;
if (slow==fast) {
return true;
}
}
return false;
}
}
检查链表是否有环
最新推荐文章于 2024-04-22 20:33:24 发布