I am not sure if this solution will work or maybe I misunderstood your code completely.
Consider a list [1,2,3,4,5].Let's say the random pointer of 1 point to 5.
You code creates a new node 5 and then when it comes to 4 it again creates another node 5. 4 points to a node with a value 5 but 1 doesn't point to the same node. 1 points to a different node which also has the value 5.
Am I missing something here?
@Mino-De You are right. I checked my code again and I realized that I need to create these nodes first and then link them between what I have created. Thanks a lot