// @lc code=start /* // Definition for a Node. class Node { public: int val; Node* next; Node* random; Node(int _val) { val = _val; next = NULL; random = NULL; } }; */
classSolution { public: Node* copyRandomList(Node* head){ if (head == nullptr) returnnullptr; unordered_map<Node*, Node*> nodeMap; for (auto *cur = head; cur != nullptr; cur = cur->next) { nodeMap[cur] = new Node(cur->val); } for (auto *cur = head; cur != nullptr; cur = cur->next) { nodeMap[cur]->next = nodeMap[cur->next]; nodeMap[cur]->random = nodeMap[cur->random]; } return nodeMap[head]; } }; // @lc code=end