-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloneLinkedList.py
More file actions
31 lines (25 loc) · 788 Bytes
/
CloneLinkedList.py
File metadata and controls
31 lines (25 loc) · 788 Bytes
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
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if head is None:
return None
new_node = {}
new_head = Node(head.val)
new_node[head] = new_head
p1 = head
p2 = new_head
while p1.next is not None:
p2.next = Node(p1.next.val)
new_node[p1.next] = p2.next
p2 = p2.next
p1 = p1.next
p1 = head
while p1 is not None:
if p1.random is not None:
new_node[p1].random = new_node[p1.random]
p1 = p1.next
return new_head