package leetCoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class LeetCode141 {
public boolean hasCycle(ListNode head){
Set<ListNode> set = new HashSet<>();
while (head!=null){
if (set.contains(head)){
return true;
}
set.add(head);
head = head.next;
}
return false;
}
public boolean hasCycle1(ListNode head){
if (head == null || head.next == null){
return false;
}
ListNode first = head;
ListNode fast = head.next;
while (first!=fast){
if (fast == null || fast.next == null){
return false;
}
first = first.next;
fast = fast.next.next;
}
return true;
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-6420.html