扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
using namespace std;
class Solution {public:
Node* copyRandomList(Node* head) {vectornode_vec;//位置到节点的映射
mapnode_map;//地址到位置的映射
Node* ptr = head;
int i = 0;
while(ptr){//生成新节点,放入列表
node_vec.push_back(new Node(ptr->val));
node_map[ptr] = i;
i++;
ptr = ptr->next;
}
node_vec.push_back(NULL);
ptr= head;
i=0;
while(ptr){//新节点依次相连
node_vec[i]->next = node_vec[i+1];
if(ptr->random){//通过map,找出random到位置的映射
int node = node_map[ptr->random];
node_vec[i]->random = node_vec[node];
}
else node_vec[i]->random = NULL;
ptr = ptr->next;
i++;
}
return node_vec[0];
}
};
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流