当前位置:网站首页>[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]
边栏推荐
- MATLAB小技巧(28)模糊綜合評價
- ECMAScript6介绍及环境搭建
- Bluebridge cup internet of things competition basic graphic tutorial - clock selection
- Tips 1: Web video playback code
- Business modeling of software model | overview
- JS asynchronous error handling
- 资源变现小程序添加折扣充值和折扣影票插件
- Lori remote control LEGO motor
- Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
- Halcon affine transformations to regions
猜你喜欢

EA introduction notes

Arduino+a4988 control stepper motor

猜谜语啦(9)

深度学习模型与湿实验的结合,有望用于代谢通量分析

Matlab tips (28) fuzzy comprehensive evaluation

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?

How apaas is applied in different organizational structures

Sword finger offer 09 Implementing queues with two stacks

Lori remote control LEGO motor

Redis实现高性能的全文搜索引擎---RediSearch
随机推荐
Is the security account given by Yixue school safe? Where can I open an account
Bit operation related operations
暑假第一周
Meizu Bluetooth remote control temperature and humidity access homeassistant
Adaboost使用
How apaas is applied in different organizational structures
Example 007: copy data from one list to another list.
特征工程
ABC#237 C
猜谜语啦(142)
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?
猜谜语啦(4)
C# LINQ源码分析之Count
An enterprise information integration system
Guess riddles (3)
Tips 1: Web video playback code
猜谜语啦(2)
IT冷知识(更新ing~)
Sword finger offer 06 Print linked list from end to end
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘