您现在的位置是:首页 >其他 >【力扣】138.随机链表的复制网站首页其他
【力扣】138.随机链表的复制
简介【力扣】138.随机链表的复制
AC截图
题目
代码
使用哈希存储<旧节点,新结点>
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
if(head==NULL){
return NULL;
}
unordered_map<Node*,Node*> map;
Node* p=head;
while(p!=NULL){
map[p]=new Node(p->val);
p=p->next;
}
p=head;
while(p!=NULL){
map[p]->next = map[p->next];
map[p]->random = map[p->random];
p=p->next;
}
p=head;
return map[p];
}
};
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。