-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy-list-with-random-pointer.py
47 lines (38 loc) · 1.12 KB
/
copy-list-with-random-pointer.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
# Leetcode 138. Copy List with Random Pointer
#
# Link: https://leetcode.com/problems/copy-list-with-random-pointer/
# Difficulty: Medium
# Complexity:
# O(N) time | where N represent the number of elements in the linked list
# O(1) space
"""
# Definition for a Node.
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: 'Optional[Node]') -> 'Optional[Node]':
node = head
if not head:
return head
while node:
new_node = Node(node.val, node.next)
node.next = new_node
node = new_node.next
node = head
while node:
if node.random:
node.next.random = node.random.next
node = node.next.next
node = head
copy_head = head.next
result = copy_head
while result.next:
node.next = node.next.next
node = node.next
result.next = result.next.next
result = result.next
return copy_head