-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path138.py
25 lines (24 loc) · 805 Bytes
/
138.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
# https://neetcode.io/problems/copy-linked-list-with-random-pointer
# https://leetcode.com/problems/copy-list-with-random-pointer/description/
"""
# 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
from typing import Optional
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
nodes = {}
h = head
while h:
nodes[h] = Node(h.val)
h = h.next
h = head
while h:
nodes[h].next = nodes[h.next] if h.next else None
nodes[h].random = nodes[h.random] if h.random else None
h = h.next
return nodes[head] if head else None