A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
Return the head of the copied linked list.
The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
val: an integer representingNode.valrandom_index: the index of the node (range from0ton-1) that therandompointer points to, ornullif it does not point to any node.
Your code will only be given the head of the original linked list.
Example 1:

Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]] Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:

Input: head = [[1,1],[2,1]] Output: [[1,1],[2,1]]
Example 3:

Input: head = [[3,null],[3,0],[3,null]] Output: [[3,null],[3,0],[3,null]]
Constraints:
0 <= n <= 1000-104 <= Node.val <= 104Node.randomisnullor is pointing to some node in the linked list.
My Solution:
""" # 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 next_list = [head] node = head while node.next: if node.next in next_list: break next_list.append(node.next) node = node.next random_indexes = [] for node in next_list: random_indexes.append(None if node.random is None else next_list.index(node.random)) copy_head = Node(head.val) if len(next_list) == 1: copy_head.next = None if head.next is None else copy_head copy_head.random = None if head.random is None else copy_head return copy_head copy_list = [] node = copy_head for i in range(len(next_list)): node.next = None if i == len(next_list) - 1 else Node(next_list[i + 1].val) copy_list.append(node) node = node.next for i in range(len(copy_list)): copy_list[i].random = None if random_indexes[i] is None else copy_list[random_indexes[i]] return copy_head

ChatGPT's Solution:
""" # 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 # Step 1: Create new nodes and interweave them with original nodes curr = head while curr: new_node = Node(curr.val, curr.next) curr.next = new_node curr = new_node.next # Step 2: Assign random pointers for the new nodes curr = head while curr: if curr.random: curr.next.random = curr.random.next curr = curr.next.next # Step 3: Separate the original and copied list curr = head new_head = head.next copy_curr = new_head while curr: curr.next = curr.next.next copy_curr.next = copy_curr.next.next if copy_curr.next else None curr = curr.next copy_curr = copy_curr.next return new_head


浙公网安备 33010602011771号