复制带随机指针的链表-链表138-python&c++

算法思想

  • 哈希表Mydic映射原有节点->新的节点
  • 原节点为空,则返回空
  • 原节点在哈希表中可以找到,则说明新的节点已生成,直接返回
  • 根据原有节点的值,创建新的节点root = Node(node.val)
  • 将原有节点和新节点的对应关系添加到哈希表中Mydic[node] = root
  • 最后参照原节点的next和random关系,创建新的next和random节点给新节点root
  • 递归整个过程

python

# Definition for a Node.
class Node:
    def __init__(self, x: int, next=None, random=None):
        self.val = int(x)
        self.next = next
        self.random = random


class Solution:
    def copyRandomList(self, head):
        dic = dict()

        def recursion(node):
            if not node:
                return node
            if node in dic:
                return dic[node]
            
            root = Node(node.val)
            dic[node] = root
            root.next = recursion(node.next)
            root.random = recursion(node.random)

            return root
        
        return recursion(head)

c++

/*
// 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) {
        Node* dummy = new Node(-1);
        Node* pre = dummy;
        unordered_map<int, int> index_random;
        unordered_map<Node*, int> index_node;
        Node* curr = head;
        int index = 0;

        // 记录原链表各节点位置索引
        while (curr != nullptr) {
            index_node[curr] = ++index;
            curr = curr->next;
        }

        // 记录原链表各节点的random位置索引
        curr = head;
        index = 0;
        while (curr != nullptr) {
            if (curr->random == nullptr) {
                index_random[++index] = -1;
            } else {
                index_random[++index] = index_node[curr->random];
            }
            curr = curr->next;
        }

        // 构造新链表
        // 先只还原next指针,不还原random指针
        curr = head;
        while (curr != nullptr) {
            Node* newNode = new Node(curr->val);
            pre->next = newNode;
            pre = pre->next;
            curr = curr->next;
        }

        // 还原random指针
        // 使用之前的random位置索引,用于遍历定位random位置
        curr = dummy->next;
        index = 0;
        while (curr != nullptr) {
            int k = index_random[++index];
            if (k == -1) {
                curr->random = nullptr;
            } else {
                Node* curr2 = dummy;
                while (k != 0) {
                    curr2 = curr2->next;
                    k--;
                }
                curr->random = curr2;
            }
            curr = curr->next;
        }

        return dummy->next;
    }

};

和python部分的代码思路一样,都是老新节点存在哈希表中,递归构建新链表。

/*
// 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:
    unordered_map<Node*, Node*> old_new_node_map;
    Node* copyRandomList(Node* head) {
        return recursion(head);
    }

    Node* recursion(Node* node) {
        if (node == nullptr) return node;
        if (old_new_node_map.count(node) > 0) return old_new_node_map[node];

        Node* newNode = new Node(node->val);
        old_new_node_map[node] = newNode;
        newNode->next = recursion(node->next);
        newNode->random = recursion(node->random);

        return newNode;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值