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

相关推荐
源代码•宸6 分钟前
分布式缓存-GO(分布式算法之一致性哈希、缓存对外服务化)
开发语言·经验分享·分布式·后端·算法·缓存·golang
yongui478341 小时前
MATLAB的指纹识别系统实现
算法
高山上有一只小老虎1 小时前
翻之矩阵中的行
java·算法
jghhh011 小时前
RINEX文件进行卫星导航解算
算法
爱思德学术1 小时前
中国计算机学会(CCF)推荐学术会议-A(计算机科学理论):LICS 2026
算法·计算机理论·计算机逻辑
CVHub1 小时前
多模态图文训推一体化平台 X-AnyLabeling 3.0 版本正式发布!首次支持远程模型推理服务,并新增 Qwen3-VL 等多款主流模型及诸多功能特性,等
算法
hoiii1872 小时前
MATLAB实现Canny边缘检测算法
算法·计算机视觉·matlab
qq_430855882 小时前
线代第二章矩阵第四课:方阵的幂
算法·机器学习·矩阵
roman_日积跬步-终至千里2 小时前
【计算机设计与算法-习题2】动态规划应用:矩阵乘法与钢条切割问题
算法·矩阵·动态规划
kupeThinkPoem2 小时前
计算机算法导论第三版算法视频讲解
数据结构·算法