My clean C++ code,very easy to understand.The point is not to duplicate nodes

  • 0
    class Solution {
        RandomListNode *copyRandomList(RandomListNode *head) {
            unordered_map<RandomListNode*,RandomListNode*> record;
            return copyList(head,record);
        RandomListNode *copyList(RandomListNode   *node,unordered_map<RandomListNode*,RandomListNode*> &record){
            if(node == nullptr) return nullptr;
            if(record.empty() || record.find(node) == record.end()){
               RandomListNode *newNode = new RandomListNode(node->label);
               record[node] = newNode;
               newNode->next = copyList(node->next,record);
               newNode->random = copyList(node->random,record);
               return newNode;
                return record[node];

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.