(Java) linked list OJ question---LeetCode 138 Copy linked list with random pointer

content

1. Topic description 

2. Method 1 analysis and reference code

3. Method 2 analysis and reference code


1. Topic description 

Topic link: Copy a linked list with random pointers

Question: 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.

Example:

Take an example yourself:

2. Method 1 analysis and reference code

We use the concept of linked list to do it, and the specific method is divided into three steps:

1. Copy the linked list node to link , and after copying the link, it will look like this:

2. Assign a random pointer field to the new node to be copied , and the length of the assignment is as follows:

3. Disconnect the copied new linked list from the original linked list as follows:

Reference Code:

class Solution {
    public Node copyRandomList(Node head) {
        //判空操作,如果链表为空直接返回
        if(head == null){
            return head;
        }
        //复制链接
        Node cur = head;
        while(cur != null){
            Node newNode = new Node(cur.val);//以原链表的值new新节点
            newNode.next = cur.next;
            cur.next = newNode;
            cur = newNode.next;
        }
        //给复制的节点赋随机值
        cur = head;
        Node newNode = cur.next;
        while(cur != null){
            if(cur.random != null){
                newNode.random = cur.random.next;
            }
            cur = newNode.next;
            if(cur != null){
                newNode = cur.next;
            }
        }
        //将复制的链表从原链表当中断开
        cur = head;
        Node newHead = cur.next;//新链表头节点
        Node newCur = newHead;//新链表节点的引用
        while(cur != null){
            cur.next = newCur.next;
            cur = newCur.next;
            if(cur != null){
                newCur.next = cur.next;
                newCur = cur.next;
            }
        }
        return newHead;
    }
}

3. Method 2 analysis and reference code

We use the data structure of hashmap to do it. The specific method is divided into three steps:

1. Traverse the original linked list, set the node of the original linked list as K, the newly copied node as V, and store K and the corresponding V in the hashmap , after which it is as follows:

2. Link the nodes of V in the hashmap in sequence , and the link is as follows:

3. Assign random pointer fields to the nodes of V in the hashmap in turn . After completion, it will look like this:

 

Reference Code:

class Solution {
    public Node copyRandomList(Node head) {
        Map<Node,Node> m = new HashMap<>();
        Node cur = head;
        //hashmap中存放的K-V为:原链表的节点-新复制的节点
        while(cur != null){
            m.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while(cur != null){
            m.get(cur).next = m.get(cur.next);//将V中的节点链接起来
            m.get(cur).random = m.get(cur.random);//给V中的节点赋随机指针域
            cur = cur.next;
        }
        return m.get(head);
    }
}

Guess you like

Origin blog.csdn.net/qq_58710208/article/details/122143671