【leetcode】-关于复制带随机指针的链表

作者:小树苗渴望变成参天大树
作者宣言:认真写好每一篇博客
作者gitee:gitee
在这里插入图片描述

如 果 你 喜 欢 作 者 的 文 章 ,就 给 作 者 点 点 关 注 吧!

文章目录


前言

hello,各位友友们,今天我们不讲新的知识点,我们来讲一期题目,我挑选的leetcode的题目,这个题目做起来不是很难,但是不好分析。思路顺了,代码自然就写出来了。


在这里插入图片描述
这个题目乍一看就是链表的复制,创建一个链表,然后遍历原链表把数据拷贝过去就好了

在这里插入图片描述

我们发现把数据域和next域可以很好的复制过去,但是random域没法很好的复制过去,有点人就说拿我们通过数据域来判断random指向哪一个节点不就好了,但是数据域使可以重复的,那这个时候我们又怎么来找random指向的节点呢??

1.我们可以使用这样的方式,把所有节点链接到原节点的后面,把数据域拷贝过去,
在这里插入图片描述
我们来看这部分的代码

	struct Node*cur=head;
    //插入拷贝节点到原链表上
    while(cur)
    {
    
    
        struct Node*next=cur->next;
        struct Node*copy=(struct Node*)malloc(sizeof(struct Node));
        copy->val=cur->val;
        cur->next=copy;
        copy->next=next;//尾插法
        cur=next;
    }

2.我们来看看这个链接关系,我们copy节点的前一个节点的random的下一个节点就是copy节点的random
在这里插入图片描述
但是我们这里姚做一个判断,如果cur的random等于NULL,那么copy的random也等于NULL,来看这一个部分的代码:

 cur=head;
    while(cur)
    {
    
    
        struct Node*copy=cur->next;
        struct Node*next=cur->next->next;
        if(cur->random==NULL)
        {
    
    
            copy->random=NULL;
        }
        else
        {
    
    
            copy->random=cur->random->next;
        }
        cur=next;
    }

循环的往下面走,大家可以试试用走读带啊吗的方式来测试代码

扫描二维码关注公众号,回复: 14729187 查看本文章

3.把copy节点和原节点断开,就是改变链接关系,并且把恢复原链表的链接关系,即使用尾插法把copy节点保存

在这里插入图片描述
我们来一下这个部分的代码:

 cur=head;
    struct Node*copyHead=NULL;
    struct Node*copyTail=NULL;
    while(cur)
    {
    
    
        struct Node*copy=cur->next;
        struct Node*next=copy->next;
        if(copyHead==NULL)
        {
    
    
            copyHead=copyTail=copy;
        }
        else
        {
    
    
            copyTail->next=copy;
            copyTail=copy;
        }
        cur->next=next;
        cur=next;
    }

相信大家把三个部分弄懂就完全掌握了这个题目解题方式,这个题目最重要的就是第二步,把第二步理解了,问题就不大,那我们看一下这个题目完整的代码吧

struct Node* copyRandomList(struct Node* head) {
    
    
	struct Node*cur=head;
    //插入拷贝节点到原链表上
    while(cur)
    {
    
    
        struct Node*next=cur->next;
        struct Node*copy=(struct Node*)malloc(sizeof(struct Node));
        copy->val=cur->val;
        cur->next=copy;
        copy->next=next;
        cur=next;
    }
    
    cur=head;
    while(cur)
    {
    
    
        struct Node*copy=cur->next;
        struct Node*next=cur->next->next;
        if(cur->random==NULL)
        {
    
    
            copy->random=NULL;
        }
        else
        {
    
    
            copy->random=cur->random->next;
        }
        cur=next;
    }

    cur=head;
    struct Node*copyHead=NULL;
    struct Node*copyTail=NULL;
    while(cur)
    {
    
    
        struct Node*copy=cur->next;
        struct Node*next=copy->next;
        if(copyHead==NULL)
        {
    
    
            copyHead=copyTail=copy;
        }
        else
        {
    
    
            copyTail->next=copy;
            copyTail=copy;
        }
        cur->next=next;
        cur=next;
    }
    return copyHead;
}

希望这篇博客能够帮助你解决这道题目的困扰,我们下期再见了,各位友友们!

猜你喜欢

转载自blog.csdn.net/qq_69369227/article/details/129598222