就是用回溯法。用一個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];
}
};