Copy List with Random Pointer

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]':
        cache = {}
        temp = head
        while temp:
            cache[temp] = Node(temp.val, None, None)
            temp = temp.next
        
        temp = head
        while temp:
            cache[temp].next = cache.get(temp.next, None)
            cache[temp].random = cache.get(temp.random, None)
            temp = temp.next
        
        return cache.get(head)