当前位置:网站首页>[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]
边栏推荐
- Basic number theory -- Euler function
- Guess riddles (2)
- 轮子1:QCustomPlot初始化模板
- How apaas is applied in different organizational structures
- One question per day - replace spaces
- Affected tree (tree DP)
- Yolov4 target detection backbone
- L298N module use
- UE pixel stream, come to a "diet pill"!
- EA introduction notes
猜你喜欢
It cold knowledge (updating ing~)
How apaas is applied in different organizational structures
猜谜语啦(142)
leetcode - 445. Add two numbers II
RT-Thread内核快速入门,内核实现与应用开发学习随笔记
猜谜语啦(2)
Digital analog 1: linear programming
Example 007: copy data from one list to another list.
Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
猜谜语啦(3)
随机推荐
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
Example 009: pause output for one second
Basic number theory - fast power
[matlab] matlab reads and writes Excel
Guess riddles (9)
GEO数据库中搜索数据
Search data in geo database
Pytorch entry record
Chapter 18 using work queue manager (1)
Typical low code apaas manufacturer cases
[noi simulation] juice tree (tree DP)
RT-Thread内核快速入门,内核实现与应用开发学习随笔记
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
Matlab tips (28) fuzzy comprehensive evaluation
Run菜单解析
我从技术到产品经理的几点体会
Guess riddles (2)
某公司文件服务器迁移方案
One question per day - replace spaces
【日常訓練--騰訊精選50】557. 反轉字符串中的單詞 III