I am trying to understand what's this line doing:
Set<ListNode> nodesSeen = new HashSet<>();
This is from a solution to check if we have a cyclic linkedList or not. Could someone help me understand this line's assignment please (I bolded it in the code snippet below)?
Thank you!
public boolean hasCycle(ListNode head) {
Set<ListNode> nodesSeen = new HashSet<>();
while (head != null) {
if (nodesSeen.contains(head)) {
return true;
} else {
nodesSeen.add(head);
}
head = head.next;
}
return false;
}