leetcode || 138、Copy List with Random Pointer

problem:

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

Hide Tags

Hash Table Linked
List

题意:copy一个单链表,单链表的节点多了一个指针,随机指向一个节点或者空

thinking:

(1)这道题其实蛮简单,诀窍在于hash table的应用。

(2)使用 unordered_map<RandomListNode *, RandomListNode *> record;底层是借助hash table实现的,访问效率高。

这种存储新旧节点指针的方法在图的copy中也用到过,效率奇高!

(3)先遍历链表,将新旧节点指针存入hash table,先不管next指针和random指针。

(4)再遍历hash table,找到旧节点next和random的指向,跟新新节点的next指针和random指针。

注意先调用count()或者find()判断key值是否存在

code:

class Solution {
private:
    unordered_map<RandomListNode *, RandomListNode *> record;
    queue<RandomListNode *> _queue;
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        if(head==NULL)
            return NULL;
        _queue.push(head);
        while(!_queue.empty())
        {
            RandomListNode *tmp=_queue.front();
            _queue.pop();
            if(tmp->next!=NULL)
                _queue.push(tmp->next);
            RandomListNode *tmp2= new RandomListNode(tmp->label);
            record.insert(make_pair(tmp,tmp2));
        }
        for(unordered_map<RandomListNode *,RandomListNode *>::iterator it=record.begin();it!=record.end();++it)
        {
            RandomListNode *tmp3=it->first;
            RandomListNode *tmp4=it->second;
            if(record.count(tmp3->next)!=0)
                tmp4->next=record[tmp3->next];
            else
                tmp4->next=NULL;

            if(record.count(tmp3->random)!=0)
                tmp4->random=record[tmp3->random];
            else
                tmp4->random=NULL;

        }
        return record[head];
    }
};
时间: 2024-11-05 22:55:59

leetcode || 138、Copy List with Random Pointer的相关文章

leetcode题目:Copy List with Random Pointer

题目: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路: 主要是深层复制的问题: 本题比较简单,具体实现见代码: /** * Definition for singly-linked list with a ran

LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 这题用map做其实比较简单,但是一开始没想明白越想越乱,最后看了下别人的实现,思路还是很清晰的,代码如下所示: 1 /** 2 * Definition for singl

LeetCode题解之Copy List with Random Pointer

1.题目描述 2.问题分析 首先要完成一个普通的单链表的深度复制,然后将一个旧的单链表和新的单链表的节点使用map对应起来,最后,做一次遍历即可. 3.代码 1 RandomListNode *copyRandomList(RandomListNode *head) { 2 if( head == NULL){ 3 return NULL; 4 } 5 RandomListNode* newhead = new RandomListNode(0); 6 RandomListNode* np =

leetcode -day8 Copy List with Random Pointer &amp; Single Number I II

五一中间断了几天,开始继续... 1.  Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 分析:剑指offer上的一道题目,分三步进行,首先复制每个链表结点

LeetCode: Copy List with Random Pointer [138]

[题目] A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. [题意] 给定一个链表,每个节点除了next指针外,还有一个random指针,指向任意的节点. 要求,复制这样的一个链表 [思路] 思路1: 先一次生成每个节点对

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

[leetcode]Copy List with Random Pointer @ Python

原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 解题思路:这题主要是需要深

Copy List with Random Pointer leetcode java

题目: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 题解: 如果要copy一个带有random pointer的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描li

133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab