[LeetCode]-138. 随机链表的复制

目录

 题目

解题步骤

1.拷贝节点插入原节点的后面

2.置每个拷贝节点random

3.拷贝节点解下来,尾插到一起,恢复原链表

完整代码


 题目

给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。

构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 

例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。

返回复制链表的头节点。

用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:

  • val:一个表示 Node.val 的整数。
  • random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。

你的代码  接受原链表的头节点 head 作为传入参数。

示例:

[LeetCode]-138. 随机链表的复制_第1张图片

[LeetCode]-138. 随机链表的复制_第2张图片

[LeetCode]-138. 随机链表的复制_第3张图片

解题步骤

1.拷贝节点插入原节点的后面

给原链表的每个节点都拷贝一份,其中每个新节点的值都设为其对应的原节点的值,将新节点都分别插入到原节点的后面,组成了一个新链表。

如下图:

[LeetCode]-138. 随机链表的复制_第4张图片

 代码

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;
    }

2.置每个拷贝节点random

由于新链表节点上的random指向状态要和原链表节点上的相同,因此原节点后面的拷贝出来的节点的random指向的节点应该是原节点random指向节点的后面拷贝节点,置每个拷贝节点random可以用copy->random=cur->random->next,(原节点random置空时除外,此时拷贝节点random也置空)。

如下图:

[LeetCode]-138. 随机链表的复制_第5张图片

代码

cur=head;
while(cur)
{
    struct Node* copy=cur->next;

     //置copy->random
    if(cur->random==NULL)
        copy->random=NULL;
    else
        copy->random=cur->random->next;

    cur=copy->next;
}

3.拷贝节点解下来,尾插到一起,恢复原链表

接下来就是单链表的节点的删除和尾插的内容了,先创建一个拷贝拷贝链表的头copyhead,创建copytail用于保存解下的拷贝节点,创建copy用来遍历解开拷贝的节点,创建单链表删除节点时需要的两边节点cur、next,随着拷贝节点向后遍历,cur和next一起到null时结束遍历。

如下图:

[LeetCode]-138. 随机链表的复制_第6张图片

代码

cur=head;
struct Node* copyhead=NULL,*copytail=NULL;
while(cur)
{
    struct Node* copy=cur->next;
    struct Node* next=copy->next;

    //copy节点尾插到新链表
    if(copytail==NULL)
    {
        copyhead=copytail=copy;
    }
    else
    {
        copytail->next=copy;
        copytail=copytail->next;
    }

    //恢复原链表
    cur->next=next;

    cur=next;
}
return copyhead;

}

完整代码

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;

     //置copy->random
    if(cur->random==NULL)
        copy->random=NULL;
    else
        copy->random=cur->random->next;

    cur=copy->next;
}

cur=head;
struct Node* copyhead=NULL,*copytail=NULL;
while(cur)
{
    struct Node* copy=cur->next;
    struct Node* next=copy->next;

    //copy节点尾插到新链表
    if(copytail==NULL)
    {
        copyhead=copytail=copy;
    }
    else
    {
        copytail->next=copy;
        copytail=copytail->next;
    }

    //恢复原链表
    cur->next=next;

    cur=next;
}
return copyhead;


}

你可能感兴趣的:(LeetCode刷题分享,leetcode,链表,算法)