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","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}

Explanation:
Node 1‘s value is 1, both of its next and random pointer points to Node 2.
Node 2‘s value is 2, its next pointer points to null and its random pointer points to itself.题意:给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点代码如下:
/**
 * // Definition for a Node.
 * function Node(val,next,random) {
 *    this.val = val;
 *    this.next = next;
 *    this.random = random;
 * };
 */
/**
 * @param {Node} head
 * @return {Node}
 */
var copyRandomList = function(head) {
   const dummy = new Node(NaN, null, null);
    let original = head;
    let copier = dummy; 

    const map = new Map();
    while(original) {
        let newNode = new Node(original.val, null, null);
        copier.next = newNode;
        map.set(original, newNode);

        copier = copier.next;
        original = original.next;
    }
    copier = dummy.next;
    original = head;
    while(original) {
        if (original.random) {
            copier.random = map.get(original.random)
        }
        copier = copier.next
        original = original.next
    }

    return dummy.next
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10970758.html

时间: 2024-08-01 07:20:24

138. Copy List with Random Pointer(js)的相关文章

Copy List with Random Pointer (16)

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 pointer,如果

leetcode 之Copy List with Random Pointer(23)

深拷贝一个链表,不同的是这个链表有个额外的随机指针.参考:http://blog.csdn.net/ljiabin/article/details/39054999 做法非常的巧妙,分成三步,一是新建结点,并放在旧结点之后:二是修改新结点的random指针:三是将新旧链表断开. RandomListNode *randomList(RandomListNode *head) { //复制每个结点,并将新结点放在旧结点之后 for (RandomListNode *cur = head; cur

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

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 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 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

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

【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