title: How to hash and check for equality of objects with circular references
body: <p>I have a <strong>cyclic</strong> graph-like structure that is represented by <em>Node</em> objects.
  A <em>Node</em> is either a scalar value (leaf) or a list of n>=1 <em>Nodes</em> (inner node).</p>
  
  <p>Because of the possible circular references, I cannot simply use a recursive HashCode() function, that combines the HashCode() of all child nodes: It would end up in an infinite recursion.</p>
  
  <p>While the HashCode() part seems at least to be doable by flagging and ignoring already visited nodes, I'm having some troubles to think of a working and efficient algorithm for Equals().</p>
  
  <p>To my surprise I did not find any useful information about this, but I'm sure many smart people have thought about good ways to solve these problems...right?</p>
  
  <p>Example (python): </p>
  
  <pre><code>A = [ 1, 2, None ]; A[2] = A  
  B = [ 1, 2, None ]; B[2] = B
  </code></pre>
  
  <p>A is equal to B, because it represents exactly the same graph.</p>
  
  <p>BTW. This question is not targeted to any specific language, but implementing hashCode() and equals() for the described <em>Node</em> object in Java would be a good practical example.</p>