判断单向链表有循环的算法(只给最佳答案50分)

michaellufhl 2010-08-11 03:24:21
有一个单向链表:
class Node{
Node next;
}


如何判断此链表存在循环,例如 n0->n1->n2…->ni->….->nk->ni
...全文
279 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
healer_kx 2010-08-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 michaellufhl 的回复:]

healer_kx的方案比较cool。

ps:显然还有人没理解题目。
[/Quote]

呵呵,Thanks~楼主懂就足够了~
michaellufhl 2010-08-11
  • 打赏
  • 举报
回复
healer_kx的方案比较cool。

ps:显然还有人没理解题目。
fei1703 2010-08-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 healer_kx 的回复:]
思路很简单,

你这样想,有一条环状跑道,两个人在上面跑步,一个人快,一个人慢,那么两个人迟早是要相会的。

有环链表也一样,你看成是跑道,我现在有s1和s2两个人,一个一次一步,一个一次两步。
不出一圈,两个准遇上,如果有环,没环的话,快的那个的next迟早是null~
[/Quote]
你的算法的效率如何?
zhaining522 2010-08-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 healer_kx 的回复:]

思路很简单,

你这样想,有一条环状跑道,两个人在上面跑步,一个人快,一个人慢,那么两个人迟早是要相会的。

有环链表也一样,你看成是跑道,我现在有s1和s2两个人,一个一次一步,一个一次两步。
不出一圈,两个准遇上,如果有环,没环的话,快的那个的next迟早是null~
[/Quote]


lz 说的是单向链表 。、。。。 汗

判断此链表存在循环???? 我没明白lz说的是什么意思?
判断这个链表是否正在被读? 还是这个链表不为空????

lliiqiang 2010-08-11
  • 打赏
  • 举报
回复
我有一种方法,但是只能保证单线程,即不能有两个线程同时访问链表去判断是否有环
class Node{
Node next;
boolean get=false;//标识是否已经被遍历
}
public static void main(String[] args){
Node head=...;//打头
while(head!=null){
if(head.get){
System.out.println("存在环");
return;
}
else{
head.get=true;
head=head.next;
}
}
System.out.println("不存在环");
}
这种方法也可以应用于判断分支结构中是否有环。例如在java虚拟机垃圾回收过程中判断一个对象是否已经被遍历过,从而避免因对象之间引用关系构成环而造成的死循环
闻志流e师兄 2010-08-11
  • 打赏
  • 举报
回复


class Node
{
Node next;
int value;
}

public class Main {

public static boolean isCircle(Node head) {

Node s1 = head.next;
while(s1 != null){
if(s1 == head){
return true;
}
s1 = s1.next;
}
return false;
}
}


xutao_2008_2000 2010-08-11
  • 打赏
  • 举报
回复
class Node
{
Node next;
int value;
}
//唯一值
key=Random();
int node.value=key;
while(node.next!=null){
if(node.next.value=key){
return true;
}

}
knightzhuwei 2010-08-11
  • 打赏
  • 举报
回复
经典问题啊 楼主应该再要求明确点:必须要O(1)的空间复杂度
healer_kx 2010-08-11
  • 打赏
  • 举报
回复
思路很简单,

你这样想,有一条环状跑道,两个人在上面跑步,一个人快,一个人慢,那么两个人迟早是要相会的。

有环链表也一样,你看成是跑道,我现在有s1和s2两个人,一个一次一步,一个一次两步。
不出一圈,两个准遇上,如果有环,没环的话,快的那个的next迟早是null~
朽木可雕 2010-08-11
  • 打赏
  • 举报
回复

package com.future.pang;

public class Node {

// 指向下一节点
private Node next = null;
// 存储数据
private Object obj = null;

// 判断节点是不是最后一个
public boolean hasNext()
{
return next==null?false:true;
}

// 下一个节点
public Node next()
{
return this.getNext();
}

// 添加数据
public void add(Object obj)
{
this.obj = obj;
}


// 设置节点
public void setNext(Node node)
{
this.next = node;
}
// 获得节点,同next()方法一样
public Node getNext()
{
return this.next;
}
}

healer_kx 2010-08-11
  • 打赏
  • 举报
回复
多好的算法的帖子啊。。。
  • 打赏
  • 举报
回复
你发布的帖子都是求代码帖!
healer_kx 2010-08-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hanvslin 的回复:]

遍历元素,一个个存到Set中,同时判断存入的元素是否已经存在,若存在则有循环
[/Quote]
你这个算法有空间复杂度。而且效率也一般。
hanvslin 2010-08-11
  • 打赏
  • 举报
回复
遍历元素,一个个存到Set中,同时判断存入的元素是否已经存在,若存在则有循环
healer_kx 2010-08-11
  • 打赏
  • 举报
回复
其他大部分代码是用于创建链表,以及制造有环链表[current.next = n5;]这一句就是产生环的。可以注释或者不注释上,来试验结果。
healer_kx 2010-08-11
  • 打赏
  • 举报
回复
实际代码是//IMPORTANT!这里。


class Node
{
Node next;
int value;
}

public class Main {

public static void printLinkedList(Node head) {
Node current = head;
while (current != null) {
System.out.print(current.value);
current = current.next;
System.out.print(" ");
}
}

//IMPORTANT!
public static boolean circleLinkedList(Node head) {

Node s1 = head.next;
Node s2 = (head.next != null) ? head.next.next : null;

while (s1 != s2) {
if (s1 == null || s1.next == null || s2 == null || s2.next == null)
return false; //Has the tail

s1 = s1.next;
s2 = s2.next.next;
}

return true;
}

public static void main(String[] args) {

Node head = null;
Node current = null;
Node n5 = null;
for (int i = 0; i < 10; i++)
{
Node n = new Node();
if (head != null) {
n.value = i;
current.next = n;
current = n;
} else {
head = n;
current = n;
current.next = null;
current.value = i;
}

if (i == 5) {
n5 = current;
}
}

printLinkedList(head);

current.next = n5;

System.out.println(circleLinkedList(head));

}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧