Take a list: A -> B -> C, where A.random = C, B.random = A, C.random = null.
Pass : create clones. Map: {A: A', B: B', C: C'}. Each clone has the correct value but no pointers yet.
Pass : wire pointers.
- A:
A'.next = map[B] = B'.A'.random = map[C] = C'. - B:
B'.next = map[C] = C'.B'.random = map[A] = A'. - C:
C'.next = map[null] = null.C'.random = map[null] = null.
Result: A' -> B' -> C' with correct random pointers, no references to original nodes.
You traverse the list twice, so time. The hash map stores entries, so space.