-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy List with Random Pointer
43 lines (38 loc) · 1.15 KB
/
Copy List with Random Pointer
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
"""
# Definition for a Node.
class Node:
def __init__(self, x, next=None, random=None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution(object):
def copyRandomList(self, head):
"""
:type head: Node
:rtype: Node
"""
if not head:
return None
# First pass: Create new nodes and interleave them with original nodes
current = head
while current:
new_node = Node(current.val, current.next, None)
current.next = new_node
current = new_node.next
# Second pass: Assign random pointers for the copied nodes
current = head
while current:
if current.random:
current.next.random = current.random.next
current = current.next.next
# Third pass: Separate the interleaved lists
current = head
new_head = head.next
while current:
copy = current.next
current.next = copy.next
current = current.next
if copy.next:
copy.next = copy.next.next
return new_head