Detecting a loop in a Linked list using a hashset I was wondering if detecting a loop in a linked list using a HashSet is better than using two pointers?

        HashSet&lt;Integer&gt; set = new HashSet&lt;Integer&gt;();
    LinkedListNode current = head;
    while(current != null) {
        if(current.next != null) {
            if(set.contains(current.next.data)) {
                return current.next; // this has been seen so there is a loop.
            }
            set.add(current.next.data);
        }
        current = current.next;
    }
    return null;
I think instead of adding integers to the Hashset, what we should do is insert a LinkedListNode instance into the Hashset and then to detect a loop we check whether the current node exists in the Hashset or not, if it does then there's a loop

 HashSet&lt;LinkedListNode&gt; set = new HashSet&lt;LinkedListNode&gt;();
    LinkedListNode current = head;
    while(current != null) {
        if(current.next != null) {
            if(set.contains(current.next)) {
                return current.next; // this has been seen so there is a loop.
            }
            set.add(current.next);
        }
        current = current.next;
    }
    return null;