leetcode_138 随机链表的复制

1. 题意

就是复制链表,不过链表多了一个random指针,

它随机的指向链表中的元素,或者是一个空值。

2. 题解

如果是普通的链表,我们直接复制就好了,不过多了一个随机指针,它有可能指向后面的元素,因此我们可以用一个哈希表进行记录。

2.1 哈希表

有两种写法,一种是递归的。就是官方说的回溯。

cpp 复制代码
class Solution {
public:
    unordered_map<Node*, Node*> cachedNode;

    Node* copyRandomList(Node* head) {
        if (head == nullptr) {
            return nullptr;
        }
        if (!cachedNode.count(head)) {
            Node* headNew = new Node(head->val);
            cachedNode[head] = headNew;
            headNew->next = copyRandomList(head->next);
            headNew->random = copyRandomList(head->random);
        }
        return cachedNode[head];
    }
};

另外一种是迭代的写法

cpp 复制代码
/*
// 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 *nHead = NULL;
        Node *pre   = NULL;

        map<Node *, Node *> pr;
        pr[NULL] = NULL;

        for (Node *cur = head; cur != NULL; cur = cur->next) {
            Node *ncur = new Node(cur->val);
            pr[cur] = ncur;
            
            if ( pre == NULL) {
                nHead = ncur;
            }
            else {
                pre->next = ncur;
            }
            pre = ncur;
        }

        for (Node *cur = head; cur != NULL; cur = cur->next ) {
            pr[cur]->random = pr[cur->random]; 
        }

        return nHead;
    }
};
2.2 奇偶链表

这种解法是在0x3f的题解里面看到的,

我自己感觉跟哈希表其实是一样的,

只是这里取了一下巧。

具体做法就是,把每一个复制的链表节点给链接到老链表的后面。

这样其实就可以通过next来实现和哈希表一样的功能了!

最后再把链表给断开就好了。

cpp 复制代码
class Solution {
public:
    Node* copyRandomList(Node* head) {
        
        Node *nxt = NULL;
        for (Node * cur = head; cur != NULL; cur = nxt) {
            nxt = cur->next;
            Node *nNode = new Node(cur->val);
            cur->next = nNode;
            nNode->next = nxt;
        }



        for (Node *cur = head; cur != NULL; cur = cur->next->next) {
            if ( cur->random != NULL) {
                cur->next->random = cur->random->next;
            }
        }



        Node *nHead = NULL;
        if (head)
            nHead = head->next;
        for (Node *cur = head; cur != NULL && cur->next != NULL; cur = nxt ) {
            nxt = cur->next;
            cur->next = nxt->next;
        }

        return nHead;
    }
};

3. 参考

leetcode
0x3f

相关推荐
_OP_CHEN5 分钟前
【算法基础篇】(四十四)数论之欧拉定理与扩展欧拉定理深度解析:从降幂到超大规模幂运算
c++·算法·蓝桥杯·算法竞赛·欧拉定理·扩展欧拉定理·acm/icpc
lfwh8 分钟前
Java 中基于 DBSCAN 算法的车辆交汇点计算实现详解
java·开发语言·算法
数据大魔方11 分钟前
【期货量化入门】期权交易入门:从零开始学期权量化(TqSdk完整教程)
数据库·python·mysql·算法·区块链·程序员创富
期货资管源码18 分钟前
期货资管分仓软件开发/平台搭建经验分享
经验分享·算法·eclipse·区块链
Xの哲學33 分钟前
Linux 实时调度机制深度解析
linux·服务器·网络·算法·边缘计算
fie888934 分钟前
基于蚁群算法求解带时间窗的车辆路径问题
数据库·人工智能·算法
ytttr87341 分钟前
基于人工蜂群算法(ABC)的MATLAB数值计算求解框架
开发语言·算法·matlab
珂朵莉MM1 小时前
2025年睿抗机器人开发者大赛CAIP-编程技能赛-高职组(国赛)解题报告 | 珂学家
java·开发语言·人工智能·算法·机器人
这周也會开心1 小时前
JVM-垃圾回收器
jvm·算法
橘颂TA1 小时前
【剑斩OFFER】算法的暴力美学——力扣 844 题:比较含退格的字符串
数据结构·c++·算法·力扣·结构与算法