package LinkedList;
public class Circle {
public static void main(String[] args) {
LNode linkedNode1 = new LNode();
linkedNode1.val = 1;
LNode linkedNode2 = new LNode();
linkedNode2.val = 2;
LNode linkedNode3 = new LNode();
linkedNode3.val = 3;
LNode linkedNode4 = new LNode();
linkedNode4.val = 4;
linkedNode1.next = linkedNode2;
linkedNode2.next = linkedNode3;
linkedNode3.next = linkedNode4;
linkedNode4.next = linkedNode2;
LNode l = begin(linkedNode1);
System.out.println("--单向链表的起始位置->" + l.val);
}
public static LNode begin(LNode linkedNode) {
LNode p = linkedNode.next;
LNode q = linkedNode.next.next;
while (q != null) {
if (p == q) {
break;
} else {
p = p.next;
q = q.next.next;
}
}
q = linkedNode;
while (q != null) {
if (p == q)
return p;// 返回环中入口结点
p = p.next;
q = q.next;
}
return null;
}
}
class LNode {
public int val;
public LNode next;
}
public class Circle {
public static void main(String[] args) {
LNode linkedNode1 = new LNode();
linkedNode1.val = 1;
LNode linkedNode2 = new LNode();
linkedNode2.val = 2;
LNode linkedNode3 = new LNode();
linkedNode3.val = 3;
LNode linkedNode4 = new LNode();
linkedNode4.val = 4;
linkedNode1.next = linkedNode2;
linkedNode2.next = linkedNode3;
linkedNode3.next = linkedNode4;
linkedNode4.next = linkedNode2;
LNode l = begin(linkedNode1);
System.out.println("--单向链表的起始位置->" + l.val);
}
public static LNode begin(LNode linkedNode) {
LNode p = linkedNode.next;
LNode q = linkedNode.next.next;
while (q != null) {
if (p == q) {
break;
} else {
p = p.next;
q = q.next.next;
}
}
q = linkedNode;
while (q != null) {
if (p == q)
return p;// 返回环中入口结点
p = p.next;
q = q.next;
}
return null;
}
}
class LNode {
public int val;
public LNode next;
}