解决了!原因是破坏了原来的链表结构。
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
if(pHead==nullptr)
return pHead;
RandomListNode* head=new RandomListNode(0);
RandomListNode* p=head;
RandomListNode* cur=pHead;
//复制
while(cur)
{
p->next=cur;
p=p->next;
cur=cur->next;
RandomListNode* newhead=new RandomListNode(p->label);
p->next=newhead;
p=p->next;
}
cur=head->next;
p=cur->next;
//链接指针
while(cur)
{
if(cur->random)
p->random=cur->random->next;
else
p->random=cur->random;
cur=p->next;
if(cur)
p=cur->next;
}
cur=head;
p=cur->next;
//拆分
RandomListNode* keep=new RandomListNode(0);
RandomListNode* tail=keep;
while(p)
{
cur->next=p->next;
cur=cur->next;
tail->next=p;
tail=p;
p=cur->next;
}
tail->next=nullptr;
pHead=keep->next;
return head->next;
}
};