[leetcode]138. Copy List with Random Pointer

来源:互联网 发布:it求职 编辑:程序博客网 时间:2024/06/06 22:46

题目链接:https://leetcode.com/problems/copy-list-with-random-pointer/tabs/description

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.



class Solution {private:    unordered_map<RandomListNode*, RandomListNode*> hmap;public:    RandomListNode *copyRandomList(RandomListNode *head) {        if (!head) return NULL;        if (hmap.find(head) != hmap.end())            return hmap.find(head)->second;        RandomListNode* node = new RandomListNode(head->label);        hmap[head] = node;        node->next = copyRandomList(head->next);        node->random = copyRandomList(head->random);        return node;    }};


原创粉丝点击