138. Copy List with Random Pointer

目录

题目描述

方法一、使用哈希表

方法二、不使用哈希表


题目描述

问题的关键是,random指针指向的是原链表的结点,这个原链表的结点对应哪一个新链表的结点呢?有两种办法。一是用哈希表。另一种是复制原链表的每一个结点,并将新结点接在原结点的后面组成一个长度加倍的链表,这样原结点的直接后继就是该原结点对应的新结点。

方法一、使用哈希表

unordered_map<Node*,Node*> new_table;//键是原链表中的旧结点,值是新链表中的新结点

unordered_map<Node*,Node*> random_table;//键是原链表中的旧结点,值是该旧结点的random指针指向的结点

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) {
        if(head == nullptr)
            return nullptr;
        unordered_map<Node*,Node*> new_table;//键是原链表中的旧结点,值是新链表中的新结点
        unordered_map<Node*,Node*> random_table;//键是原链表中的旧结点,值是该旧结点的random指针指向的结点
        Node* newHead = nullptr;
        Node* newTail = nullptr;
        Node* cur = head;

        //第一步,建立新链表
        while(cur){
            Node* node = new Node(cur->val);
            new_table.insert({cur,node});
            random_table.insert({cur,cur->random});
            if(newHead == nullptr){
                newHead = node;
                newTail = node;
            }else{
                newTail->next = node;
                newTail = node;
            }
            cur = cur->next;
        }

        //第二步,设置新链表结点的random指针
        for(const auto& [old_node,new_node] : new_table)
        {
            if(random_table[old_node]){
                new_node->random = new_table[random_table[old_node]];
            }
        }
        return newHead;
    }
};

实际上前面做法中的random_table是可以不需要的。

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) {
        if(head == nullptr)
            return nullptr;
        unordered_map<Node*,Node*> new_table;//键是原链表中的旧结点,值是新链表中的新结点
        Node* newHead = nullptr;
        Node* newTail = nullptr;
        Node* cur = head;
        //第一步,建立新链表
        while(cur){
            Node* node = new Node(cur->val);
            new_table.insert({cur,node});
            if(newHead == nullptr){
                newHead = node;
                newTail = node;
            }else{
                newTail->next = node;
                newTail = node;
            }
            cur = cur->next;
        }

        cur = head;
        Node* new_cur = newHead;
        //第二步,设置新链表结点的random指针
        while(cur){
            if(cur->random){
                new_cur->random = new_table[cur->random];
            }
            cur = cur->next;
            new_cur = new_cur->next;
        }

        return newHead;
    }
};

方法二、不使用哈希表

第一步,复制每一个旧结点,并将新结点接在旧结点的后面组成新旧结点交替出现的长度翻倍的大链表。

第二步,设置新结点的random指针。

第三步,从大链表中提取出新结点组成新链表,并将原链表复原。

需要着重强调的是,第二步和第三步必须分开来做。

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) {
        if(head == nullptr)
            return nullptr;
        Node* cur = head;
        //复制每一个旧结点,并将新结点接在旧结点的后面
        while(cur){
            Node* node = new Node(cur->val);
            Node* temp = cur->next;
            node->next = cur->next;
            cur->next = node;
            cur = temp;
        }

        cur = head;
        //设置新结点的random指针
        while(cur){
            if(cur->random)
                cur->next->random = cur->random->next;
            cur = cur->next->next;//原链表向前走一位
        }

        Node* new_head = nullptr;
        Node* new_tail = nullptr;
        cur = head;
        //提取出新结点组成新链表,并将原链表复原
        while(cur){
            if(new_head == nullptr){
                new_head = cur->next;
                new_tail = cur->next;
            }else{
                new_tail->next = cur->next;
                new_tail = cur->next;
            }
            cur->next = cur->next->next;//复原原链表
            cur = cur->next;//原链表向前走一步
        }

        return new_head;
    }
};
相关推荐
亮亮爱刷题9 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
_周游9 天前
【数据结构】_二叉树OJ第二弹(返回数组的遍历专题)
数据结构·算法
双叶8369 天前
(C语言)Map数组的实现(数据结构)(链表)(指针)
c语言·数据结构·c++·算法·链表·哈希算法
zmuy9 天前
124. 二叉树中的最大路径和
数据结构·算法·leetcode
转码的小石9 天前
Java面试复习指南:并发编程、JVM、Spring框架、数据结构与算法、Java 8新特性
java·jvm·数据结构·spring·面试·并发编程·java 8
chao_7899 天前
滑动窗口题解——找到字符串中所有字母异位词【LeetCode】
数据结构·算法·leetcode
LZA1859 天前
数据结构day1
数据结构
稳兽龙9 天前
P3258 [JLOI2014] 松鼠的新家
数据结构·c++·算法·深度优先·lca
江边垂钓者9 天前
进程间通信、线程间通信
java·网络·数据结构
XiaoCCCcCCccCcccC9 天前
C语言数组介绍 -- 一维数组和二维数组的创建、初始化、下标、遍历、存储,C99 变长数组
c语言·数据结构·算法