package link;
/**
* 判断链表中是否有环
* 近似于数学中追及相遇问题
* @author
*
*/
public class Sort1 {
/**
* 判断是否有环
* @param head 链表的头节点
* @return
*/
public static boolean isCycle(Node head) {
Node p1=head;
Node p2=head;
while(p2!=null&&p2.next!=null) {
p1=p1.next;
p2=p2.next.next;
if(p1==p2) {
return true;
}
}
return false;
}
/**
* 链表节点
* @author
*
*/
private static class Node{
int data;
Node next;
Node(int data){
this.data=data;
}
}
public static void main(String[] args) {
Node node1=new Node(5);
Node node2=new Node(3);
Node node3=new Node(7);
Node node4=new Node(2);
Node node5=new Node(6);
node1.next=node2;
node2.next=node3;
node3.next=node4;
node4.next=node5;
node5.next=node2;
System.out.println(isCycle(node1));
}
}
java判断链表是否有环
最新推荐文章于 2022-07-10 17:20:57 发布