标签:script esc etc ext int 题目 指针 amp head
LeetCode 138. Copy List with Random Pointer
又是copy 指针的题目。
这个和上一道题目有个坑点,函数中的参数要加&地址符。
class Solution {
public:
RandomListNode* ans;
map<int,RandomListNode*> m;
RandomListNode *copyRandomList(RandomListNode *head) {
if(head==NULL)
return ans;
dfs(ans,head);
return ans;
}
void dfs(RandomListNode* &ans,RandomListNode *head)
{
if(head==NULL) return;
if(m[head->label]==NULL)
{
m[head->label] = new RandomListNode(head->label);
}
ans = new RandomListNode(head->label);
if(head->random==NULL)
ans->random = NULL;
else
{
if(m[head->random->label]==NULL)
m[head->random->label] = new RandomListNode(head->random->label);
ans->random = m[head->random->label];
}
dfs(ans->next,head->next);
}
};
LeetCode 138 Copy List with Random Pointer
标签:script esc etc ext int 题目 指针 amp head
原文地址:https://www.cnblogs.com/dacc123/p/9963356.html