forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.py
63 lines (49 loc) · 1.37 KB
/
24.py
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
# -*- coding:utf-8 -*-
class RandomListNode:
def __init__(self, x):
self.label = x
self.next = None
self.random = None
class Solution:
# 返回 RandomListNode
def Clone(self, pHead):
# write code here
currentNode = pHead
if pHead is None:
return None
while currentNode is not None:
node = RandomListNode(currentNode.label)
node.next = currentNode.next
currentNode.next = node
currentNode = currentNode.next.next
currentNode = pHead
while currentNode is not None:
node = currentNode.next
if currentNode.random is not None:
node.random = currentNode.random.next
currentNode = node.next
currentNode = pHead
cloneP = pHead.next
while currentNode.next is not None:
temp = currentNode.next
currentNode.next = temp.next
currentNode = temp
return cloneP
head = RandomListNode(1)
node2 = RandomListNode(2)
node3 = RandomListNode(3)
node4 = RandomListNode(4)
node5 = RandomListNode(5)
nodex = RandomListNode('#')
head.next = node2
head.random = node3
node2.next = node3
node2.random = node5
node3.next = node4
node3.random = nodex
node4.next = node5
node4.random = node2
node5.random = nodex
s = Solution()
h = s.Clone(head)
a = 0