leetcode 链接:复制带随机指针的链表

题目

给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。
构造这个链表的 深拷贝。 深拷贝应该正好由 n全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点

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

返回复制链表的头节点。

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

val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0n-1);如果不指向任何节点,则为 null
你的代码 接受原链表的头节点 head 作为传入参数。

示例 1:
[中等] 138. 复制带随机指针的链表 - 图1

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

示例 2:
[中等] 138. 复制带随机指针的链表 - 图2

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

示例 3:
[中等] 138. 复制带随机指针的链表 - 图3

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

示例 4:

输入:head = []
输出:[]
解释:给定的链表为空(空指针),因此返回 null。

解答 & 代码

解法一:哈希表

  • 设置一个哈希表 unordered_map<Node*, Node*> nodeMap,用于存储 <原链表节点指针,对应的拷贝节点指针> 键值对
  • 遍历原链表各节点,生成拷贝链表对应节点,同时设置拷贝链表各节点的 next 指针;同时,将 <原链表节点指针,对应的拷贝节点指针> 键值对存储到哈希表
  • 同时遍历两个链表,来设置拷贝链表各节点的 random 指针:假设当前遍历到原链表的 curOri 节点,遍历到拷贝链表的 curNew 节点,那么 curNew->random = nodeMap[curOri->random]

时间复杂度 O(N),空间复杂度 O(N)

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;

    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head == NULL)    // 特殊情况:原链表为空,则拷贝的链表也为空,返回 NULL
            return NULL;

        unordered_map<Node*, Node*> nodeMap;    // 哈希表,存储<原链表节点指针,对应的拷贝节点指针>
        Node* dummyHead = new Node(-1);            // 拷贝链表的虚拟头节点
        Node* pre = dummyHead;
        Node* curOri = head;                    // 原链表的当前节点
        // 遍历原链表,生成拷贝链表(只设置next连接,暂不设置random连接)
        // 并将<原链表节点指针, 其对应的拷贝节点指针> 键值对存储到哈希表
        while(curOri != NULL)
        {
            Node* node = new Node(curOri->val);
            nodeMap.insert(make_pair(curOri, node));
            pre->next = node;
            pre = node;
            curOri = curOri->next;
        }

        curOri = head;                            // 原链表的当前节点
        Node* curNew = dummyHead->next;            // 拷贝链表的当前节点
        // 同时遍历两个链表,设置拷贝链表各节点的 random 指针
        while(curOri != NULL)
        {
            curNew->random = curOri->random == NULL ? NULL : nodeMap[curOri->random];
            curOri = curOri->next;
            curNew = curNew->next;
        }

        return dummyHead->next;
    }
};

执行结果:

执行结果:通过

执行用时:4 ms, 在所有 C++ 提交中击败了 98.63% 的用户
内存消耗:11.1 MB, 在所有 C++ 提交中击败了 42.71% 的用户

解法二:不用额外数据结构

★★☆☆复制含有随机指针节点的链表
时间复杂度 O(N),空间复杂度 O(1)

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;

    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head == NULL)
            return NULL;

        // 1. 遍历链表,生成每个节点的副本并插在该节点的后面
        Node* curOri = head;
        Node* curNew = NULL;
        while(curOri != NULL)
        {
            curNew = new Node(curOri->val);
            Node* next = curOri->next;
            curNew->next = next;
            curOri->next = curNew;
            curOri = next;
        }

        // 2. 遍历链表,为每个副本节点设置 random 指针
        curOri = head;
        while(curOri != NULL)
        {
            curNew = curOri->next;
            curNew->random = (curOri->random == NULL) ? NULL : curOri->random->next;
            curOri = curOri->next->next;
        }

        // 3. 分离原链表和副本链表
        Node* dummyHead = new Node(-1);
        Node* pre = dummyHead;
        curOri = head;
        while(curOri != NULL)
        {
            curNew = curOri->next;
            pre->next = curNew;
            pre = curNew;
            curOri->next = curOri->next->next;
            curOri = curOri->next;
        }
        return dummyHead->next;
    }
};

执行结果:

执行结果:通过

执行用时:12 ms, 在所有 C++ 提交中击败了 61.85% 的用户
内存消耗:11.1 MB, 在所有 C++ 提交中击败了 66.72% 的用户