就是用回溯法。用一个hash_map来存储访问过的节点。
class Solution {
private:
unordered_map<Node*, Node*> all;
public:
Node* copyRandomList(Node* head) {
if(head == NULL) return NULL;
if(all.count(head) == 0) {
all[head] = new Node(head->val);
all[head]->next = copyRandomList(head->next);
all[head]->random = copyRandomList(head->random);
//return newNode;
}
return all[head];
}
};