Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
tech-cow
leetcode
Repository navigation
Code
Issues
26
(26)
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
leetcode
/
linkedlist
/
copyRandomList.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
43 lines (38 loc) · 1.85 KB
master
Breadcrumbs
leetcode
/
linkedlist
/
copyRandomList.py
Copy path
Top
File metadata and controls
Code
Blame
43 lines (38 loc) · 1.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 138. Copy List with Random Pointer
# Author: Yu Zhou
# A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
# Return a deep copy of the list
# Definition for singly-linked list with a random pointer.
# class RandomListNode(object):
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None
# 思路:
# 这题其实就是拷贝所有的链表到一个新的
# 唯一的难点是如何在拷贝玩所有链表之后,还能够将Random的指针指对
# 所以这里用的方法是用Hash,然后第一次遍历用来建新的Node
# 第二次遍历用来对Hash里面新建的Node进行指针的赋值
class Solution(object):
def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
dic = dict()
m = n = head
#第一次循环用来创建所有原本链表里的Node,并且对他们进行赋值
#需要搞懂的是,这一次循环,新的链表是一个一个单独的Node,他们的指针都还没有赋值
while m:
dic[m] = RandomListNode(m.label)
m = m.next
#第二次循环,用来对这一个一个独立的Node进行他们的指针赋值,其中包括每个链接下一个Node的指针
#以及这道题的另一个Random的指针
while n:
#这里dic[n].next,dic[n]就是key "n" 相对应的val,也就是之前创建的,RandomListNode(m.label)
#然后我们对这个新创建的独立Node,设置他的next pointer
# dic.get(n.next)里面的n.next是reference原List里面的Node的next,不要搞混了
dic[n].next = dic.get(n.next)
dic[n].random = dic.get(n.random)
n = n.next
return dic.get(head)
You can’t perform that action at this time.