这个怎么解决?

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;
        //拆分
       while(p)
       {
           cur->next=p->next;
           cur=cur->next;
           delete p;
           p=cur->next;
       }
        return head->next;
    }
};
结果:
 空.请检查一下你的代码,有没有循环输入处理多个case.点击查看如何处理多个case
这是源问题:
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
我的代码有问题还是啥?我觉得我的代码没问题啊?模拟了好几次了.
求大佬解答!!
#C/C++#
全部评论
解决了!原因是破坏了原来的链表结构。 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;     } };
点赞 回复 分享
发布于 2019-11-03 23:59
牛客做题不是要把答案print出来吗
点赞 回复 分享
发布于 2019-11-03 23:32

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务