剑指offer:26复杂链表的复制(注释)

请实现 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。

方法一:哈希表

 代码语言:cpp 

STL:unordered_map用法

/*
// 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* curr = head;
        unordered_map <Node*, Node*> map;
        while(curr!=nullptr){
            map[curr] = new Node(curr->val);    //遍历所有结点并复制,建立原结点到新结点的键值对映射
            curr = curr->next;
        }
        curr = head;
        while(curr!=nullptr){
            map[curr]->next = map[curr->next];  //利用哈希表查询,构建新结点的next指向
            map[curr]->random = map[curr->random];  //构建新结点的random的指向
            curr = curr->next;
        }
        return map[head];   //返回新链表的头结点
    }
};

18 / 18 个通过测试用例

状态:通过

执行用时: 4 ms

内存消耗: 11 MB

方法二:拆分拼接 

思路:

 

 代码语言: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 * curr = head;
        while(curr != nullptr){ //复制结点,拼接
            Node *copy = new Node(curr->val);
            Node *behd = curr->next;
            curr->next = copy;
            copy->next = behd;
            curr = copy->next;
        }
        curr = head;
        while(curr != nullptr){ //构建random指向
            if(curr->random != nullptr)
                curr->next->random = curr->random->next;
            curr = curr->next->next;
        }
        curr = head->next;
        Node * res = curr;
        while(curr->next != nullptr){   //拆分成两个链表
            head->next = head->next->next;
            curr->next = curr->next->next;
            head = head->next;
            curr = curr->next;
        }
        head->next = nullptr;   //原链表的尾结点(原则上不处理也可以,但是报错)
        //Next pointer of node with label 1 from the original list was modified. 
        return res; //新链表头结点
    }
};

 18 / 18 个通过测试用例

状态:通过

执行用时: 12 ms

内存消耗: 10.9 MB

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

For L9L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值