C++算法(七)复杂链表的复制

30 篇文章 2 订阅
15 篇文章 0 订阅


复杂链表的复制

一、题目描述

请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

示例 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]]

示例 4:

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

提示:

-10000 <= Node.val <= 10000
Node.random 为空(null)或指向链表中的节点。
节点数目不超过 1000 。

作者:Krahets
链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9p0yy1/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

二、解题思路及代码实现

1、解题思路

我的实现方法是:借助map和vector容器,第一遍循环构造新链表,第二遍循环处理random指针。之所以使用map,是因为它的查找效率非常高,使用vector是其随机访问速度快。我的实现运行速度快,缺点是消耗内存较多,不如官方的方法节约内存。

2、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* ret = NULL;
        Node* current = NULL;
        map<Node*, int> index_map_old;
        vector<Node*> new_nodes;
        Node* tmp_old = head;
        Node* tmp_new = ret;
        int index = 0;
        new_nodes.reserve(50);

        //构建新链表、保存旧链表节点索引、保存新节点指针
        while (tmp_old != NULL) {
            Node* tmp = new Node(tmp_old->val);
            tmp->next = NULL;

            if (ret == NULL) {
                ret = tmp;
                current = tmp;
            }

            if (current != tmp) {
                current->next = tmp;
                current = tmp;
            }

            index_map_old[tmp_old] = index++;
            new_nodes.push_back(tmp);
            tmp_old = tmp_old->next;
        }

        //实现random指针赋值
        tmp_old = head;
        tmp_new = ret;
        while (tmp_old != NULL) {
            if (tmp_old->random == NULL) {
                tmp_new->random = NULL;
            } else {
                int index_tmp = index_map_old[tmp_old->random];
                tmp_new->random = new_nodes[index_tmp];
            }

            tmp_old = tmp_old->next;
            tmp_new = tmp_new->next;
        }

        return ret;
    }
};

三、提交结果

在这里插入图片描述


总结

从提交结果看,效率确实是挺高的,但内存使用较多,想了一下,又没有发现优化方案,查看官方解题思路,确实是又快又省内存。观摩一下官方解决方案:

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head == nullptr) return nullptr;
        Node* cur = head;
        unordered_map<Node*, Node*> map;
        // 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
        while(cur != nullptr) {
            map[cur] = new Node(cur->val);
            cur = cur->next;
        }
        cur = head;
        // 4. 构建新链表的 next 和 random 指向
        while(cur != nullptr) {
            map[cur]->next = map[cur->next];
            map[cur]->random = map[cur->random];
            cur = cur->next;
        }
        // 5. 返回新链表的头节点
        return map[head];
    }
};

作者:Krahets
链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9plk45/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值