【Likou 138】Copy linked list with random pointer

Problem description: Give you a linked list of length n, each node contains an additional random pointer random, which can point to any node in the linked list or an empty node.

Constructs a deep copy of this linked list. A deep copy should consist of exactly n new nodes, where the value of each new node is set to the value of its corresponding original node. The next pointer and random pointer of the new node should also point to the new node in the copied linked list, so that these pointers in the original linked list and the copied linked list can represent the same linked list state. None of the pointers in the copied linked list should point to the nodes in the original linked list .

For example, if there are two nodes X and Y in the original linked list, where X.random --> Y . Then the corresponding two nodes x and y in the replication linked list also have x.random --> y .

Returns the head node of the replicated linked list.

A linked list in input/output is represented by a linked list consisting of n nodes. Each node is represented by a [val, random_index]:

val: An integer representing Node.val.
random_index: The index of the node pointed to by the random pointer (ranging from 0 to n-1); null if it does not point to any node.
Your code only accepts the head node of the original linked list as an incoming parameter.

Example 1:
insert image description here

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:

insert image description here

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

insert image description here

Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]]

Tips :

  1. 0 <= n <= 1000
  2. -104 <= Node.val <= 104
  3. Node.random is null or points to a node in the linked list.

Topic link problem-
solving idea:
From the title, we know that the linked list has 3 domains at this time. Suppose a linked list has 4 nodes whose addresses are 111, 222, ,333, and 444 respectively. The state diagram is as follows: insert image description here
and random The pointer field can point to any location, if the random pointer field of the first node points to the third node, and the random pointer field of the second node points to itself. The random pointer fields of the third and fourth nodes are all null: insert image description here
Suppose we set the values ​​of the data value field to 1, 2, 3, and 4 respectively: insert image description here
this problem requires us to generate a new linked list, so that its structure is the same as the above picture. The structure is the same as in , the specific ideas and diagrams are as follows (detailed steps in the notes)insert image description here

class Solution {
    
    
    public Node copyRandomList(Node head) {
    
    
        if(head == null) return null;//首先应该判空
        HashMap<Node,Node> map = new HashMap<>();//定义key和value域都为节点类型的哈希表
        Node cur = head;//定义一个cur节点
        while(cur != null) {
    
    //第一次遍历链表
            //遍历原链表时,只要cur不为空,就new一个新节点,其data值与原链表的data一样
            //而next域和随机指针域不设置初始值,让其初始值为null即可
            Node node = new Node(cur.val);
            map.put(cur,node);//建立一个链表,让原链表的cur值作为key,让新链表对应位置的
            //node值作为value
            cur = cur.next;//cur继续往后走
        }
        cur = head;
        while(cur != null) {
    
    //第二次遍历
            //以第一次循环为例,将555节点的next域改为666
            map.get(cur).next = map.get(cur.next);
            //将555节点的random随机指针域改为333对应的777
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head); //返回头节点即可
    }
}

Guess you like

Origin blog.csdn.net/Mubei1314/article/details/122812699