-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path138.py
More file actions
22 lines (22 loc) · 761 Bytes
/
Copy path138.py
File metadata and controls
22 lines (22 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
if not head:
return head
newHead = RandomListNode(0)
currentNode, curr = newHead, head
nodeDict = {}
while curr:
tempNode = RandomListNode(curr.label)
nodeDict[curr] = tempNode
currentNode.next = tempNode
currentNode, curr = currentNode.next, curr.next
curr, currentNode = head, newHead.next
while curr and currentNode:
if curr.random:
currentNode.random = nodeDict[curr.random]
curr, currentNode = curr.next, currentNode.next
return newHead.next