判断单向链表有循环的算法(只给最佳答案50分) 有一个单向链表:class Node{ Node next;}如何判断此链表存在循环,例如 n0->n1->n2…->ni->….->nk->ni 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 实际代码是//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)); }} 其他大部分代码是用于创建链表,以及制造有环链表[current.next = n5;]这一句就是产生环的。可以注释或者不注释上,来试验结果。 遍历元素,一个个存到Set中,同时判断存入的元素是否已经存在,若存在则有循环 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; }} 思路很简单,你这样想,有一条环状跑道,两个人在上面跑步,一个人快,一个人慢,那么两个人迟早是要相会的。有环链表也一样,你看成是跑道,我现在有s1和s2两个人,一个一次一步,一个一次两步。不出一圈,两个准遇上,如果有环,没环的话,快的那个的next迟早是null~ class Node{ Node next; int value;}//唯一值key=Random();int node.value=key;while(node.next!=null){ if(node.next.value=key){ return true; }} 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; }} 我有一种方法,但是只能保证单线程,即不能有两个线程同时访问链表去判断是否有环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虚拟机垃圾回收过程中判断一个对象是否已经被遍历过,从而避免因对象之间引用关系构成环而造成的死循环 lz 说的是单向链表 。、 汗判断此链表存在循环???? 我没明白lz说的是什么意思?判断这个链表是否正在被读? 还是这个链表不为空???? healer_kx的方案比较cool。ps:显然还有人没理解题目。 呵呵,Thanks~楼主懂就足够了~ java 问题 有什么java api 是和矢量图相关的吗? 这样为什么能够编译通过呢? 不是final 变量不允许改变吗 又是一个初学JAVA的小疑问,请帮助我! 文件名改名出错?请过来看看; 有关applet的问题 再来一个问题,怎样判断email地址是否合理,正确? 请问怎样把ResultSet保存到数组中? 请问大家用什么开发java程序,我基本上学会了java语法,不知用什么来开发它? 出错处理? 循环字符串算法问题(50分只给最佳答案) 线程退出run方法,是否代表该线程结束
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)); }
}
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;
}
}
不出一圈,两个准遇上,如果有环,没环的话,快的那个的next迟早是null~
{
Node next;
int value;
}
//唯一值
key=Random();
int node.value=key;
while(node.next!=null){
if(node.next.value=key){
return true;
}}
{
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;
}
}
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虚拟机垃圾回收过程中判断一个对象是否已经被遍历过,从而避免因对象之间引用关系构成环而造成的死循环
判断这个链表是否正在被读? 还是这个链表不为空????
呵呵,Thanks~楼主懂就足够了~