-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyListWithRandomPointer.py
More file actions
42 lines (38 loc) · 1.13 KB
/
copyListWithRandomPointer.py
File metadata and controls
42 lines (38 loc) · 1.13 KB
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
# 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
class Solution(object):
def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
if not head: return None
myhead = RandomListNode(head.label)
dic = {}
dic[head] = myhead
p = head.next
while(p):
print p.label
newNode = RandomListNode(p.label)
dic[p] = newNode
p = p.next
p = head
while(p):
print p.label
if p.next:
dic[p].next = dic[p.next]
else:
dic[p].next = None
if p.random:
dic[p].random = dic[p.random]
else:
dic[p].random = None
p = p.next
return myhead