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非空,我们就可以在copy中找到相应的newHead.random = nodeDict[pointer.random]。

 1 # Definition for singly-linked list with a random pointer.
 2 # class RandomListNode(object):
 3 #     def __init__(self, x):
 4 #         self.label = x
 5 #         self.next = None
 6 #         self.random = None
 7
 8 class Solution(object):
 9     def copyRandomList(self, head):
10         """
11         :type head: RandomListNode
12         :rtype: RandomListNode
13         """
14         nodeDict = {}
15         dummy = RandomListNode(0)
16         newHead, pointer = dummy, head
17         while pointer:
18             Node = RandomListNode(pointer.label)
19             newHead.next = nodeDict[pointer] = Node
20             pointer, newHead = pointer.next, newHead.next
21         pointer, newHead = head, dummy.next
22         while pointer:
23             if pointer.random:
24                 newHead.random = nodeDict[pointer.random]
25             newHead, pointer = newHead.next, pointer.next
26         return dummy.next
27         

思路二:来自bittiger。只扫一遍。在每个节点检查是不是 pointer, pointer.next, pointer.random已经在nodeDict里面了,如果不在的话,先把需要的节点添加到nodeDict中。

 1 # Definition for singly-linked list with a random pointer.
 2 # class RandomListNode(object):
 3 #     def __init__(self, x):
 4 #         self.label = x
 5 #         self.next = None
 6 #         self.random = None
 7
 8 class Solution(object):
 9     def copyRandomList(self, head):
10         """
11         :type head: RandomListNode
12         :rtype: RandomListNode
13         """
14         if not head:
15             return head
16         nodeDict = {}
17         pointer = head
18
19         while pointer:
20             if pointer not in nodeDict:
21                 node = RandomListNode(pointer.label)
22                 nodeDict[pointer] = node
23
24             if pointer.next and pointer.next not in nodeDict:
25                 nextnode = RandomListNode(pointer.next.label)
26                 nodeDict[pointer.next] = nextnode
27
28             if pointer.random and pointer.random not in nodeDict:
29                 randomnode = RandomListNode(pointer.random.label)
30                 nodeDict[pointer.random] = randomnode
31
32             if pointer.next:
33                 nodeDict[pointer].next = nodeDict[pointer.next]
34             if pointer.random:
35                 nodeDict[pointer].random = nodeDict[pointer.random]
36
37             pointer = pointer.next
38
39         return nodeDict[head]
40
41     

思路:在每一个node后面都接上一个自己的copy。可以避免使用Hash table。

时间: 2024-10-01 04:44:00

Leetcode 138. Copy List with random pointer的相关文章

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 ----- 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复制带有随机指针的链表

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

【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

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

leetcode -day8 Copy List with Random Pointer & 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上的一道题目,分三步进行,首先复制每个链表结点

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

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

【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中,然后第