一、题目
二、代码
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
//快慢相遇
public class Solution
{
public boolean hasCycle(ListNode head)
{
if(head==null) return false;
ListNode slow = head;
ListNode fast = head;
while(fast!=null)
{
fast = fast.next;
if(fast!=null)
{
fast = fast.next;
slow = slow.next;
if(fast ==slow)
{
return true;
}
}
}
return false;
}
}