环路检测

47 阅读1分钟
package org.example.type.link;  
  
import java.util.HashSet;  
  
public class DelectCycleSolution {  


    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;  
    }  
}