当前位置:网站首页>[sword finger offer] sword finger offer 35 Replication of complex linked list
[sword finger offer] sword finger offer 35 Replication of complex linked list
2022-06-12 21:52:00 【Caicai 2022】
subject
Code
Execution time :48 ms, In all Python3 Defeated in submission 18.68% Users of
Memory consumption :15.7 MB, In all Python3 Defeated in submission 73.78% Users of
Pass the test case :18 / 18
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head: return
dic = {
}
# 3. Copy each node , And establish “ Original node -> New node ” Of Map mapping
cur = head
while cur:
dic[cur] = Node(cur.val)
cur = cur.next
cur = head
# 4. Building new nodes next and random Point to
while cur:
dic[cur].next = dic.get(cur.next)
dic[cur].random = dic.get(cur.random)
cur = cur.next
# 5. Return the head node of the new linked list
return dic[head]
【 Method 2】
Execution time :32 ms, In all Python3 Defeated in submission 94.64% Users of
Memory consumption :15.7 MB, In all Python3 Defeated in submission 73.78% Users of
Pass the test case :18 / 18
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head: return
cur = head
while cur:
newNode=Node(cur.val)
newNode.next=cur.next
cur.next=newNode
cur=newNode.next
cur=head
while cur:
if cur.random:
cur.next.random=cur.random.next
cur=cur.next.next
ans=cur=head.next
pre=head
while cur.next:
pre.next=pre.next.next
cur.next=cur.next.next
pre=pre.next
cur=cur.next
pre.next=None
return ans
边栏推荐
- My struggle: my years in foreign enterprises (1)
- Zip compression decompression
- What are thread scheduler and timeslicing?
- Preliminary use of jvisualvm
- Lambda expression and flow optimization code
- Cloning PDB with ADG standby
- February 27th
- SQL调优指南笔记10:Optimizer Statistics Concepts
- MySQL体系结构及基础管理(二)
- PCB package download website recommendation and detailed usage
猜你喜欢

Pixel level reconstruction and restoration technology to solve severe image blur

Ansible-大总结(六)

SQL调优指南笔记6:Explaining and Displaying Execution Plans

Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?

PE安装win10系统

User guide for JUC concurrency Toolkit

如何自己动手写一个vscode插件,实现插件自由!

How to write a vscode plug-in by yourself to realize plug-in freedom!

SQL调优指南笔记11:Histograms

Cookies and sessions
随机推荐
jsonUtils
Icml2022 | galaxy: active learning of polarization map
PCB package download website recommendation and detailed usage
SQL tuning guide notes 9:joins
Thread safe level
User guide for JUC concurrency Toolkit
复杂系统如何检测异常?北卡UNCC等最新《复杂分布式系统中基于图的深度学习异常检测方法综述》,阐述最新图异常检测技术进展
六月集训(第12天) —— 链表
Experiment 7-2-6 print Yanghui triangle (20 points)
Zip compression decompression
Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
What are thread scheduler and timeslicing?
SQL tuning guide notes 15:controlling the use of optimizer statistics
【QNX Hypervisor 2.2 用戶手册】4.2 支持的構建環境
Graphics2D类基本使用
SQL调优指南笔记18:Analyzing Statistics Using Optimizer Statistics Advisor
MySql主从复制
MySQL master-slave replication
NIO使用指南
Jin AI her power | impact tech, she can