-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCopyListWithRandomPointer_v0.py
More file actions
68 lines (53 loc) · 1.3 KB
/
CopyListWithRandomPointer_v0.py
File metadata and controls
68 lines (53 loc) · 1.3 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# encoding: utf-8
# Definition for singly-linked list with a random pointer.
class RandomListNode:
def __init__(self, x):
self.label = x
self.next = None
self.random = None
def __repr__(self):
return "<{}({}):{}:{}>".format(self.label, id(self), id(self.next), id(self.random))
def printl(n):
print n
if n.next:
printl(n.next)
class Solution:
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
d = {}
h = head
nh = None
while h:
n = RandomListNode(h.label)
if not nh:
nh = n
d[h] = n
h = h.next
h = head
while h:
if h.next:
d[h].next = d[h.next]
if h.random:
d[h].random = d[h.random]
h = h.next
return nh
if __name__ == '__main__':
s = Solution()
n1 = RandomListNode(1)
n2 = RandomListNode(2)
n3 = RandomListNode(3)
n4 = RandomListNode(4)
head = n1
n1.next = n2
n2.next = n3
n3.next = n4
n1.random = n4
n2.random = n2
n3.random = n2
n4.random = None
new_head = s.copyRandomList(head)
printl(head)
print '-' * 6
printl(new_head)