method A quick and slow node way, if the fast node meets with the slowly node proves to have a ring
public int Hasloop (node node) {
int hasloop = 0;
Node Node1 = node;
Node Node2 = node;
while (Node2 = null && node2.next! = null) {
node1 = Node1.next;
Node2 = Node2.next.next;
if (Node1.equals (Node2)) {//Meet proof with ring
hasloop = 1;
break;
}
}
return hasloop;
}
method 22 Nodes Node1 and Node2,node2 each time to start from the beginning, see if Node1 and Node2 walk the number of steps is consistent, if the inconsistency exists ring
public int hasLoop2 (node node) {int hasloop = 0;
int pos1 = 0;
Node Node1 = node;
while (Node1! = null) {node Node2 = node;
int pos2 = 0;
while (node2! = null) {if (Node2.equals (Node1)) {//Encounters if (pos1! = Pos2) {//traversed path inconsistent proof ring
Hasloop = 1;
return hasloop;
}else {break;
}} pos2++;
Node2 = Node2.next;
} pos1++;
Node1 = Node1.next;
} return Hasloop; }