Skip to main content
Improved lay-out
Link
janos
  • 113.1k
  • 15
  • 154
  • 396

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;
            }
        }

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;
            }
        }

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;
    }
}
Source Link
user103053
user103053

Detecting cycle in LinkedList

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;
            }
        }