1.题目
思路
利用快慢指针,快指针每次走两次,慢指针每次走一次,其实只要两个指针的步数不一致即可
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null){
return false;
}
ListNode front = head;
ListNode foot = head;
while(front!=null && foot != null){
foot = foot.next;
if(foot!=null){
foot = foot.next;
}
if(foot == front){
return true;
}
front = front.next;
}
return false;
}
}