Back to writing

138. Copy List With Random Pointer

138. Copy List with Random Pointer

Medium


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]]

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 <= 104
  • Node.random is null or is pointing to some node in the linked list.

 """
# 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 __init__(self):
        self.hashset = {}

    def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
        if head is None:
            return None


        if head in self.hashset:
            return self.hashset[head]


        node = Node(head.val)
        self.hashset[head] = node

        node.next = self.copyRandomList(head.next)
        node.random = self.copyRandomList(head.random)    

        return node

𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 𝗞𝗲𝘆 𝗖𝗼𝗻𝗰𝗲𝗽𝘁𝘀:

  1. Scalability: https://lnkd.in/gpge_z76
  2. Latency vs Throughput: https://lnkd.in/g_amhAtN
  3. CAP Theorem: https://lnkd.in/g3hmVamx
  4. ACID Transactions: https://lnkd.in/gMe2JqaF
  5. Rate Limiting: https://lnkd.in/gWsTDR3m
  6. API Design: https://lnkd.in/ghYzrr8q
  7. Strong vs Eventual Consistency: https://lnkd.in/gJ-uXQXZ
  8. Distributed Tracing: https://lnkd.in/d6r5RdXG
  9. Sync vs Async Communication: https://lnkd.in/gC3F2nvr
  10. Batch vs Stream Processing: https://lnkd.in/g4_MzM4s
  11. Fault Tolerance: https://lnkd.in/dVJ6n3wA

𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 𝗕𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗕𝗹𝗼𝗰𝗸𝘀:

  1. Database: https://lnkd.in/gti8gjpz
  2. Horizontal vs Vertical Scaling: https://lnkd.in/gAH2e9du
  3. Caching: https://lnkd.in/gC9piQbJ
  4. Distributed Caching: https://lnkd.in/g7WKydNg
  5. Load Balancing: https://lnkd.in/gQaa8sXK
  6. SQL vs NoSQL: https://lnkd.in/g3WC_yxn
  7. Database Scaling: https://lnkd.in/gAXpSyWQ
  8. Data Replication: https://lnkd.in/gVAJxTpS
  9. Data Redundancy: https://lnkd.in/gNN7TF7n
  10. Database Sharding: https://lnkd.in/gMqqc6x9
  11. Database Indexes: https://lnkd.in/gCeshYVt
  12. Proxy Server: https://lnkd.in/gi8KnKS6
  13. WebSocket: https://lnkd.in/g76Gv2KQ
  14. API Gateway: https://lnkd.in/gnsJGJaM
  15. Message Queues: https://lnkd.in/gTzY6uk8

𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀:

  1. Event-Driven Architecture: https://lnkd.in/dp8CPvey
  2. Client-Server Architecture: https://lnkd.in/dAARQYzq
  3. Serverless Architecture: https://lnkd.in/gQNAXKkb
  4. Microservices Architecture: https://lnkd.in/gFXUrz_T

𝗟𝗼𝘄-𝗟𝗲𝘃𝗲𝗹 𝗗𝗲𝘀𝗶𝗴𝗻 𝗣𝗿𝗼𝗯𝗹𝗲𝗺𝘀:

  1. Design Parking Lot: https://lnkd.in/dQaAuFd2
  2. Design Splitwise: https://lnkd.in/dF5fBnex
  3. Design Chess Validator: https://lnkd.in/dfAQHvN4
  4. Design Distributed Queue | Kafka: https://lnkd.in/dQ6_B4_M

Share with someone who might need this.

FAANGTips