leetcode138_复制带随机指针的链表

Map<Node, Node> map = new HashMap<>();
public Node copyRandomList(Node head) {
    if(head == null) return null;
    if(!map.containsKey(head)) {
        Node headNew = new Node(head.val);
        map.put(head, headNew);
        headNew.next = copyRandomList(head.next);
        headNew.random = copyRandomList(head.random);
    }
    return map.get(head);
}
posted @ 2022-02-09 21:56  明卿册  阅读(24)  评论(0)    收藏  举报