c#:
// A code block
public bool HasCycle(ListNode head)
{ ListNode fast = head;
ListNode slow = head;
while (fast != null && slow != null && fast.next != null)
{
fast = fast.next.next;
slow = slow.next;
if (fast == slow){
return true;
}
}
return false;
}
go:
// A code block
func hasCycle(head *ListNode) bool {
fast,slow:=head,head
for (fast!=nil&&slow!=nil&&fast.Next!=nil){
fast,slow=fast.Next.Next,slow.Next
if (fast==slow){
return true
}
}
return false
}