package org.example.type.link;
import java.util.HashSet;
public class DelectCycleSolution {
public ListNode delectCycle(ListNode head){
ListNode pos=head;
HashSet<ListNode> nodes = new HashSet<>();
while (pos!=null){
if(nodes.contains(pos)){
return pos;
}else{
nodes.add(pos);
}
pos=pos.next;
}
return null;
}
public ListNode dele(ListNode head){
if(head==null)return null;
ListNode s=head,f=head;
while (f!=null){
s=s.next;
if(f.next!=null){
f=f.next.next;
}else{
return null;
}
if(f==s){
ListNode prt=head;
while (prt!=s){
prt=prt.next;
s=s.next;
}
return prt;
}
}
return null;
}
}
环路检测双指针
最新推荐文章于 2024-10-16 10:13:45 发布