#include <map>
#include <iostream>
struct RandomListNode
{
int label;
RandomListNode *next,*random;
RandomListNode(int x):label(x),next(NULL),random(NULL)
{
}
};
class Solution
{
public:
RandomListNode* copyRandomList(RandomListNode* head)
{
RandomListNode* cpHead=NULL;
RandomListNode* pcHead=NULL;
RandomListNode* temHead=head;
std::map<RandomListNode*,RandomListNode*> atoaMap;
while(NULL!=head)
{
if(NULL==cpHead)
{
pcHead=cpHead=new RandomListNode(head->label);
atoaMap[head]=pcHead;
head=head->next;
}
else
{
pcHead->next=new RandomListNode(head->label);
atoaMap[head]=pcHead->next;
head=head->next;
pcHead=pcHead->next;
}
}
pcHead=cpHead;
while(temHead!=NULL)
{
if(temHead->random!=NULL)
{
pcHead->random=atoaMap[temHead->random];
}
pcHead=pcHead->next;
temHead=temHead->next;
}
return cpHead;
}
};
int main(int argc,char* argv[])
{
return 0;
}
带有随机指针的链表拷贝
最新推荐文章于 2021-03-30 09:34:34 发布