Java代码题:复制带随机指针的链表(哈希)

给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点,要求返回这个链表的深拷贝。

即:
复制带随机节点的单链表
用哈希记录对应关系,代码如下

/*
class Node {
    int val;
    Node next;
    Node random;
    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/
class Solution {
    public Node copyRandomList(Node head) {
        Map<Node,Node> map = new HashMap<>();
        Node cur = head;
        //将旧节点作为key,新节点作为value,放入map中
        while(cur!=null){
            Node node = new Node(cur.val);
            map.put(cur,node);
            cur= cur.next;
        }
        cur = head;
        while(cur!=null){
        	//根据map中的对应关系,连接新链表
            map.get(cur).next=map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur= cur.next;
        }
        return map.get(head);
    }
}

核心代码

            map.get(cur).next=map.get(cur.next);
            map.get(cur).random = map.get(cur.random);

map.get(cur)获取cur对应的值,即得到新链表的node节点, map.get(cur).next拿到node.next进行赋值,
map.get(cur.next)拿到cur.next对应的新节点,并将此赋给node.next.

random的赋值,和next赋值过程相同,不再解释。

发布了54 篇原创文章 · 获赞 6 · 访问量 4805

猜你喜欢

转载自blog.csdn.net/glpghz/article/details/104412579