Java代码解决LinkedList结构成环问题及相关四问
一、LinkedList链表结构是否存在环结构
Java代码实现Linkedlist链表结构及成环
public class Test01 {
static class Node{
int value;
Node next;
}
public static void main(String[] args) {
Node top=new Node();
Node p1=top;
for (int i = 1; i <=10 ; i++) {
p1.value=i;
if(i==10){
break;
}
p1.next=new Node();
p1=p1.next;
}
p1=top;
for (int i = 0; i < 3 - 1; i++) {
p1=p1.next;
}
p1.next=p1.next.next;
p1=top;
for (int i = 0; i <=10; i++) {
if(p1==null){
break;
}
System.out.println(p1.value);
p1=p1.next;
}
System.out.println("=============");
p1=top;
for (int i = 0; i < 3 - 1; i+&