题目:
这道题我使用了快慢指针的方法:
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null){
return false;
}
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null ){
slow = slow.next;
fast = fast.next.next;
if(fast == slow){
return true;
}
}
return false;
}
}
快慢指针,快指针每次比慢多走一个, 如果追上慢指针说明有环,否则无环