当前位置:网站首页>[牛客网刷题 Day4] JZ35 复杂链表的复制
[牛客网刷题 Day4] JZ35 复杂链表的复制
2022-07-05 08:39:00 【strawberry47】
题目
解答:
看不懂题目,好像输入和输出一样??
哦!原来是每个节点后面跟了一个next指针和random指针哦
想法:
用一个list存储所有的random节点,再把他们加到常规链表后面。
但是null节点没法加next了,而且一开始dummy=pHead,那就一直有random存在。。。
啊啊w(゚Д゚)w,原来我搞错题意了!!题目是要求深拷贝,并不是把节点串起来呀!
思路:建立一个字典,key是当前node的值,value是random的值;然后遍历这个字典。
class RandomListNode:
def __init__(self, x):
self.label = x
self.next = None
self.random = None
class Solution:
# 返回 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
参考答案:
思路有点类似,也是创建了一个哈希表,key是当前节点,value是节点的值;
然后再遍历一遍原始链表,把原来的next和random都取出来
class Solution:
# 返回 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]
边栏推荐
- Guess riddles (10)
- MySQL之MHA高可用集群
- 如何写Cover Letter?
- 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?
- Go dependency injection -- Google open source library wire
- Apaas platform of TOP10 abroad
- Bluebridge cup internet of things competition basic graphic tutorial - clock selection
- Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
- Business modeling | process of software model
- 整形的分类:short in long longlong
猜你喜欢
MHA High available Cluster for MySQL
Meizu Bluetooth remote control temperature and humidity access homeassistant
Guess riddles (5)
Old Wang's esp8266 and old Wu's ws2818 light strip
Sword finger offer 06 Print linked list from end to end
Agile project management of project management
Stm32--- systick timer
实例009:暂停一秒输出
猜谜语啦(5)
剑指 Offer 09. 用两个栈实现队列
随机推荐
Go dependency injection -- Google open source library wire
Count the number of inputs (C language)
暑假第一周
轮子1:QCustomPlot初始化模板
Esphone Feixun DC1 soft change access homeassstant
Arduino operation stm32
MATLAB skills (28) Fuzzy Comprehensive Evaluation
關於線性穩壓器的五個設計細節
UE像素流,来颗“减肥药”吧!
Cmder of win artifact
STM32 lights up the 1.8-inch screen under Arduino IDE
[daily training] 1200 Minimum absolute difference
Reasons for the insecurity of C language standard function scanf
关于线性稳压器的五个设计细节
猜谜语啦(9)
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
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?
Esphone retrofits old fans
Lori remote control commissioning record
2020-05-21