LeeCode138.随机链表的复制

做题链接

. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。icon-default.png?t=N7T8https://leetcode.cn/problems/copy-list-with-random-pointer/

题目描述

给你一个长度为 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 作为传入参数。

示例 1:

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

示例 2:

输入:head = [[1,1],[2,1]]
输出:[[1,1],[2,1]]

示例 3:

输入:head = [[3,null],[3,0],[3,null]]
输出:[[3,null],[3,0],[3,null]]

提示:

  • 0 <= n <= 1000
  • -104 <= Node.val <= 104
  • Node.random 为 null 或指向链表中的节点。

解题思路

此题难点在于random指针的控制。

1.第一步,遍历原链表, 在原节点(cur)后插入该节点的拷贝节点(copy)。这样,我们可以在遍历原链表时通过该节点的next指针找到它的拷贝节点(copy = cur->next)。

2.第二步,遍历插入拷贝节点后的链表,控制random。

当原节点的random指向NULL(cur->random =  NULL),我们让拷贝节点的random指向NULL即可(copy->random = NULL);

当原节点的random指向原链表中的任意节点,我们可以通过原节点的random找到它指向的原链表中的某一个节点,在通过该节点的next指针找到它的拷贝节点,就能把原节点的拷贝节点与该节点的拷贝节点连接(copy->random = cur->random->next)。

3.第三步,再次遍历插入拷贝节点后的链表,把拷贝节点插入到新链表中。

解题代码

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *next;
 *     struct Node *random;
 * };
 */
typedef struct Node Node;
struct Node* copyRandomList(struct Node* head) {
    //空链表无须拷贝,直接返回
    if(head == NULL)
    {
        return NULL;
    }

    //第一步
	Node* cur = head;
    while(cur)
    {
        Node* copy = (Node*)malloc(sizeof(Node));
        if(copy == NULL)
        {
            perror("malloc fail");
            exit(1);
        }
        copy->val = cur->val;

        //拷贝节点插入到原节点之后
        copy->next = cur->next;
        cur->next = copy;

        cur = copy->next;
    }

    //第二步,控制random
    cur = head;
    while(cur)
    {
        Node* copy = cur->next;
        if(cur->random == NULL)
        {
            copy->random = NULL;
        }
        else
        {
            copy->random = cur->random->next;
        }
        cur = copy->next;
    }

    //第三步,把拷贝节点插入到新链表中
    Node* newhead,*newtail;
    newhead = newtail = NULL;
    cur = head;
    while(cur)
    {
        Node* copy = cur->next;
        if(newhead == NULL)
        {
            newhead = newtail = copy;
        }
        else
        {
            newtail->next = copy;
            newtail = newtail->next;
        }
        cur->next = copy->next;//恢复原链表
        cur = copy->next;
    }
    return newhead;
}

以上仅提供了解题的一种思路。感谢各位读者的支持!

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于C语言实现的单链表代码: ```c #include <stdio.h> #include <stdlib.h> //定义链表节点 typedef struct Node { int data; struct Node* next; } Node; //初始化链表 Node* initList() { Node* head = (Node*)malloc(sizeof(Node)); head->next = NULL; return head; } //清空链表 void clearList(Node* head) { Node* p, * q; p = head->next; while (p) { q = p->next; free(p); p = q; } head->next = NULL; } //求链表长度 int getLength(Node* head) { int len = 0; Node* p = head->next; while (p) { len++; p = p->next; } return len; } //遍历链表 void traverseList(Node* head) { Node* p = head->next; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } //查找元素位置 int findElement(Node* head, int value) { int pos = 0; Node* p = head->next; while (p) { pos++; if (p->data == value) { return pos; } p = p->next; } return -1; } //插入元素 void insertElement(Node* head, int value, int pos) { Node* p = head; Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = value; newNode->next = NULL; while (pos > 0 && p) { pos--; if (pos == 0) { newNode->next = p->next; p->next = newNode; } p = p->next; } } //删除元素 void deleteElement(Node* head, int value) { Node* p = head; Node* q; while (p->next) { if (p->next->data == value) { q = p->next; p->next = q->next; free(q); return; } p = p->next; } } int main() { Node* head = initList(); insertElement(head, 1, 1); insertElement(head, 2, 2); insertElement(head, 3, 3); insertElement(head, 4, 4); insertElement(head, 5, 5); printf("链表元素为:"); traverseList(head); printf("链表长度为:%d\n", getLength(head)); int pos = findElement(head, 3); if (pos == -1) { printf("元素不存在!\n"); } else { printf("元素3在链表中的位置为:%d\n", pos); } deleteElement(head, 4); printf("删除元素4后,链表元素为:"); traverseList(head); clearList(head); printf("清空链表后,链表元素为:"); traverseList(head); return 0; } ``` 这里的代码实现了单链表的初始化、清空、求链表长度、遍历、查找、插入和删除操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值