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

相关推荐
cpp_25012 小时前
P10570 [JRKSJ R8] 网球
数据结构·c++·算法·题解
cpp_25012 小时前
P8377 [PFOI Round1] 暴龙的火锅
数据结构·c++·算法·题解·洛谷
uesowys2 小时前
Apache Spark算法开发指导-Factorization machines classifier
人工智能·算法
TracyCoder1232 小时前
LeetCode Hot100(26/100)——24. 两两交换链表中的节点
leetcode·链表
季明洵2 小时前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表
shandianchengzi2 小时前
【小白向】错位排列|图文解释公考常见题目错位排列的递推式Dn=(n-1)(Dn-2+Dn-1)推导方式
笔记·算法·公考·递推·排列·考公
I_LPL2 小时前
day26 代码随想录算法训练营 回溯专题5
算法·回溯·hot100·求职面试·n皇后·解数独
Yeats_Liao2 小时前
评估体系构建:基于自动化指标与人工打分的双重验证
运维·人工智能·深度学习·算法·机器学习·自动化
only-qi2 小时前
leetcode19. 删除链表的倒数第N个节点
数据结构·链表
cpp_25012 小时前
P9586 「MXOI Round 2」游戏
数据结构·c++·算法·题解·洛谷