[LeetCode][JavaScript]Copy List with Random Pointer

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.

https://leetcode.com/problems/copy-list-with-random-pointer/


第一把直接暴力两轮遍历。第一轮遍历copy链表,用hash表记录下各个节点,第二乱遍历去赋值链表里的random对象。

然后稍稍改进了一下,一次遍历里把能的找到的random都赋上,如果还没new出来,记到missed数组中,最后把missed的节点中的random再赋上值。

旧版:

 1 var copyRandomList = function(head) {
 2     var dict = {};
 3     var originNode = head;
 4     var newNode = new RandomListNode(0);
 5     var originHead = head;
 6     var newHead = newNode;
 7     while(originNode !== null){
 8         var temp = new RandomListNode(originNode.label);
 9            dict[originNode.label] = temp;
10            newNode.next = temp;
11
12         newNode = newNode.next;
13         originNode = originNode.next;
14     }
15
16     originNode = originHead;
17     newNode = newHead.next;
18     while(originNode !== null){
19         if(originNode.random !== null){
20             newNode.random = dict[originNode.random.label];
21         }
22         newNode = newNode.next;
23         originNode = originNode.next;
24     }
25     return newHead.next;
26 };

新版:

 1 /**
 2  * Definition for singly-linked list with a random pointer.
 3  * function RandomListNode(label) {
 4  *     this.label = label;
 5  *     this.next = this.random = null;
 6  * }
 7  */
 8
 9 /**
10  * @param {RandomListNode} head
11  * @return {RandomListNode}
12  */
13 var copyRandomList = function(head) {
14     var dict = {};
15     var missed = [];
16     var originNode = head;
17     var newNode = new RandomListNode(0);
18     var originHead = head;
19     var newHead = newNode;
20     while(originNode !== null){
21         var temp = new RandomListNode(originNode.label);
22            dict[originNode.label] = temp;
23            newNode.next = temp;
24            newNode = newNode.next;
25
26            if(originNode.random !== null){
27                if(dict[originNode.random.label]){
28                    newNode.random = dict[originNode.random.label];
29                }else{
30                    newNode.random = originNode.random.label;
31                    missed.push(newNode.label);
32                }
33         }
34         originNode = originNode.next;
35     }
36     for(var i = 0; i < missed.length; i++){
37         var curr = dict[missed[i]];
38         curr.random = dict[curr.random];
39     }
40     return newHead.next;
41 };
时间: 2024-10-05 05:07:10

[LeetCode][JavaScript]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. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

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上的一道题目,分三步进行,首先复制每个链表结点

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

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 【 Copy List with Random Pointer 】 python 实现

题目: 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. 代码:Runtime: 215 ms 1 # Definition for singly-linked list with a random pointer. 2

leetCode(7):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. * struct RandomListNode

【leetcode】Copy List with Random Pointer (hard)

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. 思路: 做过,先复制一遍指针,再复制random位置,再拆分两个链表. #include <iostream> #include <vector> #incl

【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 random pointer. * class RandomListNode

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