Cycle Detection

  • + 0 comments

    C# Solution:

    static bool hasCycle(SinglyLinkedListNode head) {
    
            List<SinglyLinkedListNode> list = new List<SinglyLinkedListNode>();
            while(head!=null) 
            {
                if (list.Contains(head)) 
                {
                    return true;
                } 
                else 
                {
                    list.Add(head);
                    head = head.next;
                }
            }
            return false;
        }