(刷题记录2)随机链表的复制

题目信息:

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

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

题目参考:
力扣:138. 随机链表的复制
牛客网:JZ35 复杂链表的复制

题目思路(环境来自力扣OJ的C语言):

1.遍历一遍原链表的同时,在每个原节点后面插入一个相同的新节点,共插入 n 个新节点。

2.利用两者联系,当原节点 random 指向非空节点时,新节点 random 指向为原节点 random->next。

3.将原链表和新链表分开。

复杂度:

时间复杂度:O(n)
空间复杂度:O(1)
空间复杂度考虑的是算法本身使用的空间消耗,题目中由于拷贝产生的O(n)空间为题目本身要求,则不计入。

代码和解释:

1.遍历一遍原链表的同时,在每个原节点后面插入一个相同的新节点,共插入 n 个新节点。

typedef struct Node Node;
struct Node* copyRandomList(struct Node* head) {
    if (head == NULL)								// 为空直接返回
        return NULL;
        
    Node* cur = head;
    while (cur)      			// 当cur == NULL时退出           
    {
        Node* temp = (Node*)malloc(sizeof(Node));	// 1.创建新节点
        temp->val = cur->val;   					// 并拷贝值
        temp->next = cur->next;	// 2.将新节点temp->next 指向 cur->next
        cur->next = temp;		// 3.将cur->next 指向 temp

        cur = temp->next;		// 4.cur移动到temp->next的位置
    }


}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2.利用两者联系,当原节点 random 指向非空节点时,新节点 random 指向为原节点 random->next。

    cur = head;                                     // cur 回到头节点
    while (cur)                                     // 拷贝 random
    {
        if (cur->random == NULL)                    // 1.当cur->random为空
            cur->next->random = NULL;               // 置空
        else
            cur->next->random = cur->random->next;  // 2.当cur->random不为空

        // 可用三目操作符简写成下式
        // cur->next->random = (cur->random ? cur->random->next : NULL);

        cur = cur->next->next;                      // 3.移动到下一个原节点
    }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.将原链表和新链表分开。

    cur = head;                                     // cur 回到头节点
    Node* newHead = head->next;                     // 1.保存新节点的头
    Node* temp = newHead;
    while (cur)                                     // 将两个链表复原
    {
        cur->next = temp->next;                     // 2.恢复原节点next指向
        cur = cur->next;                            // 2.2.移动到下一个原节点

        temp->next = (cur ? cur->next : cur);       // 3.恢复新节点next指向
        temp = temp->next;                          // 3.2.移动到下一个新节点
    }

在这里插入图片描述
在这里插入图片描述

完整代码

typedef struct Node Node;
struct Node* copyRandomList(struct Node* head) {
    if (head == NULL)								// 为空直接返回
        return NULL;
        
    Node* cur = head;
    while (cur)      			// 当cur == NULL时退出           
    {
        Node* temp = (Node*)malloc(sizeof(Node));	// 1.创建新节点
        temp->val = cur->val;   					// 并拷贝值
        temp->next = cur->next;	// 2.将新节点temp->next 指向 cur->next
        cur->next = temp;		// 3.将cur->next 指向 temp

        cur = temp->next;		// 4.cur移动到temp->next的位置
    }

    cur = head;                                     // cur 回到头节点
    while (cur)                                     // 拷贝 random
    {
        if (cur->random == NULL)                    // 1.当cur->random为空
            cur->next->random = NULL;               // 置空
        else
            cur->next->random = cur->random->next;  // 2.当cur->random不为空

        // 可用三目操作符简写成下式
        // cur->next->random = (cur->random ? cur->random->next : NULL);

        cur = cur->next->next;                      // 3.移动到下一个原节点
    }

    cur = head;                                     // cur 回到头节点
    Node* newHead = head->next;                     // 1.保存新节点的头
    Node* temp = newHead;
    while (cur)                                     // 将两个链表复原
    {
        cur->next = temp->next;                     // 2.恢复原节点next指向
        cur = cur->next;                            // 2.2.移动到下一个原节点

        temp->next = (cur ? cur->next : cur);       // 3.恢复新节点next指向
        temp = temp->next;                          // 3.2.移动到下一个新节点
    }

    return newHead;         // 返回新节点的头
}
  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值