当前位置:网站首页>[daiy4] copy of JZ35 complex linked list
[daiy4] copy of JZ35 complex linked list
2022-07-05 08:43:00 【strawberry47】
subject

answer :
Can't understand the topic , Like input and output ??
Oh ! It turns out that each node is followed by a next Pointers and random Pointer oh
idea :
Use one list Store all random node , Then add them to the regular linked list .
however null Nodes cannot be added next 了 , And in the beginning dummy=pHead, Then there will always be random There is ...
Ah! w(゚Д゚)w, So I made a mistake !! The topic is asking for deep copy , It's not about connecting nodes !
Ideas : Build a dictionary ,key It is the present. node Value ,value yes random Value ; Then traverse the dictionary .
class RandomListNode:
def __init__(self, x):
self.label = x
self.next = None
self.random = None
class Solution:
# return RandomListNode
def Clone(self, pHead):
# write code here
if pHead is None:
return None
res = RandomListNode(0)
node_dict = dict()
cur = pHead
while cur:
node_dict[cur.label] = cur.random
cur = cur.next
dummy = res
for node,random1 in node_dict.items():
res.next = RandomListNode(node)
res = res.next
res.random = random1
return dummy.next
Refer to the answer :
The idea is a little similar , It also creates a hash table ,key Is the current node ,value Is the value of the node ;
Then traverse the original linked list again , Original next and random Take it out
class Solution:
# return RandomListNode
def Clone(self, pHead):
# write code here
if pHead is None:
return None
dict = {
}
cur = pHead
while cur:
dict[cur] = RandomListNode(cur.label)
cur = cur.next
cur = pHead
while cur:
dict[cur].next = dict.get(cur.next)
dict[cur].random = dict.get(cur.random)
cur = cur.next
return dict[pHead]
边栏推荐
猜你喜欢

Halcon color recognition_ fuses. hdev:classify fuses by color

Agile project management of project management

319. 灯泡开关

Business modeling of software model | stakeholders

Business modeling of software model | vision

Guess riddles (6)

资源变现小程序添加折扣充值和折扣影票插件

Business modeling | process of software model

MATLAB小技巧(28)模糊综合评价

One question per day - replace spaces
随机推荐
Reasons for the insecurity of C language standard function scanf
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
Guess riddles (5)
Typescript hands-on tutorial, easy to understand
Esphone retrofits old fans
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III
MATLAB小技巧(28)模糊綜合評價
L298N module use
Guess riddles (142)
Shift operation of complement
Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
Guess riddles (8)
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
Halcon snap, get the area and position of coins
猜谜语啦(3)
Arduino burning program and Arduino burning bootloader
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
Guess riddles (4)
Bit operation related operations
12、动态链接库,dll