Copy List with Random Pointer

Problem

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 representing Node.val
  • random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if 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]]

Intuition

The problem involves creating a deep copy of a linked list with a random pointer. The approach is to traverse the original linked list twice. In the first pass, create a copy of each node and store the mapping between original and copied nodes in a dictionary (tocopy). In the second pass, set the next and random pointers for each copied node using the mapping stored in the dictionary.

Approach

Create an empty dictionary tocopy to store the mapping between original and copied nodes.
Traverse the original linked list to create a copy of each node. For each node, create a new node with the same value and store the mapping in the tocopy dictionary.
Traverse the original linked list again to set the next and random pointers for each copied node. Use the mapping stored in the tocopy dictionary to set these pointers correctly.
Return the head of the copied linked list.

Complexity

  • Time complexity:

The time complexity is O(n), where n is the number of nodes in the linked list. The algorithm traverses the linked list twice.

  • Space complexity:

The space complexity is O(n) since the solution uses additional space to store the mapping between original and copied nodes in the tocopy dictionary.

Code

"""
# 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]':
        tocopy = {None : None}
        
        current = head
        while current:
          copy = Node(current.val)
          tocopy[current] = copy
          current = current.next

        current = head
        while current:
          copy = tocopy[current]
          copy.next = tocopy[current.next]
          copy.random = tocopy[current.random]
          current = current.next

        return tocopy[head]

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 09:20:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 09:20:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 09:20:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 09:20:04       18 阅读

热门阅读

  1. Installing GDS

    2023-12-08 09:20:04       41 阅读
  2. 【1day】金和OA某接口存在未授权访问漏洞

    2023-12-08 09:20:04       31 阅读
  3. ARM虚拟化与车联网安全应用

    2023-12-08 09:20:04       39 阅读
  4. 【RabbitMQ高级功能详解以及常用插件实战】

    2023-12-08 09:20:04       37 阅读
  5. mysql的Altas读写分离(实战配置版)

    2023-12-08 09:20:04       36 阅读
  6. iosapp网站是干什么的呢?

    2023-12-08 09:20:04       32 阅读
  7. Unity 程序运行后的日志信息路径

    2023-12-08 09:20:04       34 阅读
  8. 初学websocket有感-待研究

    2023-12-08 09:20:04       36 阅读
  9. 插件原理与开发

    2023-12-08 09:20:04       44 阅读