Is this a fair/clean way to detect a cycle in a LinkedList? Should work for both single and double LinkedLists'.
public bool IsCircular()
{
if (Head != null && Head.Next != null)
{
var slow = Head;
var fast = Head.Next;
while (slow.Next != null && fast.Next != null && fast.Next.Next != null)
{
if (slow == fast)
{
return true;
}
slow = slow.Next;
fast = fast.Next.Next;
}
return false;
}
else
{
return Head != null ? (Head == Head.Next) : false;
}
}