138. Copy List with Random Pointer (Graph, Map; DFS)

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.

struct RandomListNode {
     int label;
     RandomListNode *next, *random;
     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 };

class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        if(!head) return NULL;
        map<RandomListNode *, RandomListNode *> flag;
        RandomListNode *root = copyNode(head, flag);
        return root;
    }

    RandomListNode * copyNode(RandomListNode *source, map<RandomListNode *, RandomListNode *> &flag)
    {
        if(flag.find(source)!=flag.end())
        {
            return flag[source];
        }

        RandomListNode *target = new RandomListNode (source->label);
        flag[source]=target;
        if(source->next)
            target->next = copyNode (source->next,flag);
        if(source->random)
            target->random = copyNode (source->random,flag);
        return target;
    }
};
时间: 2024-11-24 11:32:26

138. Copy List with Random Pointer (Graph, Map; DFS)的相关文章

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

138. Copy List with Random Pointer(js)

138. 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. Example 1: Input: {"$id":"1","n

Java for LeetCode 138 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. 解题思路: 我们在Java for LeetCode 133 Clone Graph题中做过图的复制,本题和图的复制十分类似,JAVA实现如下: public Random

138 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 的题多用hashmap, 难点在于如何让遍历, 如何构建新的节点间的关系. /** * Definition for singly-linked list wit

leetcode 138. Copy List with Random Pointer ----- 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. 和第133题差不多,都是图的复制,区别在于这道题的label有可能是相同的,所以导致了map的key有可能相同,所以需要处理. 两种方法差不多.第二种更简洁. 1.在复制n

Leetcode 138. 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. 思路:deep copy的意思就是克隆.扫两遍原来的list,第一遍copy node和next.然后再扫第二遍,这是如果pointer.random非空,我们就可以在co

【LeetCode】138. 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. 提示: 此题有两种方法,一种是按照原链表next的顺序依次创建节点,并处理好新链表的next指针,同时把原节点与新节点的对应关系保存到一个hash_map中,然后第

[leedcode 138] 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 random pointer. * class RandomListNode

[leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, 每一个节点都是新建的,而不是指向就节点 这个题的难点在于:随机节点. 随机节点有可能指向后边还没有建立的节点,这就没法指. 方法一:一一对应地记录每个新旧节点的映射关系,在常规节点建立后,就去查哈希表,找到对应 新节点的旧节点的random,就是新节点的random */ if (head==nu