v2 time O(n) space O(1) Accepted

in v1 forgot to check whether node.random is null