Cycle Detection

  • + 0 comments

    again this question is bugged... the example of the cycle:

    1 -> 2 -> 3 -> 1 -> NULL
    

    1 can't point to both 1 and NULL!

    At least this time the ro class compiles!

    static bool hasCycle(SinglyLinkedListNode head) {
        var d = new HashSet<SinglyLinkedListNode>();
        while(head is not null){
            if(d.Contains(head)) return true;
            d.Add(head);
            head = head.next;
        }
        return false;
    }