1、题目描述
给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。
要求返回这个链表的 深拷贝。
我们用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:
val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。
提示:
-10000 <= Node.val <= 10000
Node.random
为空(null)或指向链表中的节点。- 节点数目不超过 1000 。
2、示例
输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
3、题解
基本思想:递归+HashMap,时间复杂度O(N),空间复杂度O(N)
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
void Init_Node(Node* head, vector<int> vec, unordered_map<int, Node*> &HashMap)
{
if (vec.size() == 0)
{
head = NULL;
return;
}
Node* p = head;
p->val = vec[0];
HashMap[p->val] = p;
for (int i = 1; i < vec.size(); i++)
{
Node* q = new Node(vec[i]);
p->next = q;
p = p->next;
HashMap[p->val] = p;
}
p->next = NULL;
}
class Solution {
public:
unordered_map<Node*, Node*> HashMap; //保存旧节点与新生成的节点之间映射
Node* copyRandomList(Node* head) {
//基本思想:递归+HashMap
if (head == NULL)
return NULL;
//已经生成的节点不再生成
if (HashMap.find(head) != HashMap.end())
return HashMap[head];
Node* cur = new Node(head->val);
HashMap[head] = cur;
//生成与旧节点相连接的节点递归生成
if (head->next != NULL)
cur->next = copyRandomList(head->next);
if (head->random != NULL)
cur->random = copyRandomList(head->random);
return cur;
}
};
int main()
{
Solution solute;
Node* head = new Node(0);
unordered_map<int, Node*> HashMap;
vector<int> vec = { 7,13,11,10,1 };
//初始化链表
Init_Node(head, vec, HashMap);
HashMap[13]->random = NULL;
HashMap[13]->random = HashMap[7];
HashMap[11]->random = HashMap[1];
HashMap[10]->random = HashMap[11];
HashMap[1]->random = HashMap[7];
Node *p = solute.copyRandomList(head);
while (p != NULL)
{
cout << p->val << " ";
p = p->next;
}
cout << endl;
return 0;
}