解决了!原因是破坏了原来的链表结构。 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;     } };
点赞 评论

相关推荐

10-25 12:05
已编辑
湖南科技大学 Java
若梦难了:我有你这简历,已经大厂乱杀了
点赞 评论 收藏
分享
牛客网
牛客企业服务