[LeetCode] C++: Intermediate Question-Linked List 138. Copy Linked List with Random Pointer

138. Copy a linked list with random pointers

Medium difficulty 478

Given a linked list, each node contains an additional random pointer, which can point to any node or empty node in the linked list.

Request to return a deep copy of this linked list 

We use a  n linked list of nodes to represent the linked list in input/output. Each node is [val, random_index] represented by one  :

  • val: An Node.val integer represented  .
  • random_index: The index of the node pointed to by the random pointer (range from  0 to  n-1); if it does not point to any node, it is   null .

 

Example 1:

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

Example 2:

输入:head = [[1,1],[2,1]]
输出:[[1,1],[2,1]]

Example 3:

输入:head = [[3,null],[3,0],[3,null]]
输出:[[3,null],[3,0],[3,null]]

Example 4:

输入:head = []
输出:[]
解释:给定的链表为空(空指针),因此返回 null。

 

prompt:

  • -10000 <= Node.val <= 10000
  • Node.random It is empty (null) or points to a node in the linked list.
  • The number of nodes does not exceed 1000.

The meaning of deep copy

Simply put, it is to construct a data structure with the same structure and value as the original, but the nodes in it are not references to the original nodes.

Must admire the solution of this big brother! ! I must also record the enlightenment this question gave me!

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head == NULL){
            return head;
        }
        unordered_map<Node*, Node*> ump;
        Node* cur = head;
        while(cur != NULL){
            Node* copy = new Node(cur->val);
            ump[cur] = copy;
            cur = cur->next;
        }
        cur = head;
        while(cur != NULL){
            ump[cur]->next = ump[cur->next];
            ump[cur]->random = ump[cur->random];
            cur = cur->next;
        }
        return ump[head];
    }
};

First of all, use map to store the linked list, the key is the old linked list node, and the value is the new node, so the value here is the linked list we want to process the deep copy. Another advantage is that the linked list index can be used as a pointer to manipulate the linked list. While operating the map, you can synchronize the acquisition of the linked list. Although it looks a bit convoluted, it is actually very easy to understand and save a lot of space.

Then, after a traversal of the linked list, the key is generated based on the original linked list node, and the node value is passed to the corresponding value as the val of the newly declared node. This is just a layer copy, without next and random. copy. Therefore, in the second round of traversal of the linked list, the key and value in the map are used, which is implemented with wonderful code as follows to complete the drawing of next and random. ump[cur] means that it is all new, only val. But after this operation, next and random are perfect! ! It's really amazing!

            ump[cur]->next = ump[cur->next];
            ump[cur]->random = ump[cur->random];

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/113530391