当前位置:网站首页>[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]
边栏推荐
- Explore the authentication mechanism of StarUML
- Matlab tips (28) fuzzy comprehensive evaluation
- 【日常训练--腾讯精选50】557. 反转字符串中的单词 III
- 特征工程
- 2022.7.4-----leetcode.1200
- Business modeling | process of software model
- 2022.7.4-----leetcode. one thousand and two hundred
- One dimensional vector transpose point multiplication np dot
- Low code platform | apaas platform construction analysis
- An enterprise information integration system
猜你喜欢

Example 009: pause output for one second

我从技术到产品经理的几点体会

【NOI模拟赛】汁树(树形DP)

猜谜语啦(142)

One question per day - replace spaces

Sword finger offer 09 Implementing queues with two stacks
![[noi simulation] juice tree (tree DP)](/img/19/bc71e8dc3958e4cb87b31423a74617.png)
[noi simulation] juice tree (tree DP)

Shift operation of complement

How to manage the performance of R & D team?

Example 006: Fibonacci series
随机推荐
每日一题——替换空格
Hello everyone, welcome to my CSDN blog!
Basic number theory - factors
Halcon snap, get the area and position of coins
Mathematical modeling: factor analysis
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
319. 灯泡开关
C语言标准函数scanf不安全的原因
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?
Halcon: check of blob analysis_ Blister capsule detection
2022.7.4-----leetcode.1200
Bluebridge cup internet of things competition basic graphic tutorial - clock selection
Business modeling of software model | vision
How to manage the performance of R & D team?
猜谜语啦(5)
Arduino burning program and Arduino burning bootloader
猜谜语啦(6)
【日常训练--腾讯精选50】557. 反转字符串中的单词 III
猜谜语啦(9)
[daily training -- Tencent selected 50] 557 Reverse word III in string