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;
}
}
检查链表是否有环
最新推荐文章于 2016-07-19 11:34:57 发布
本文介绍了一种用于检测链表中是否存在环的有效算法。通过使用快慢指针技巧,该算法可以在O(n)时间内完成检测,且仅需常数级别的额外空间。文章提供了完整的Java实现代码,并详细解释了其工作原理。
548

被折叠的 条评论
为什么被折叠?



