matteo's coding field.

LeetCode.138 Copy List with Random Pointer

字数统计: 79阅读时长: 1 min
2019/05/20 Share
题意

拷贝一个结点的所有子结点,与前面有一题一样的解法

class Solution {

    Map<Node, Node> copied = new HashMap<>();

    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        if (copied.containsKey(head)) {
            return copied.get(head);
        }
        Node temp = new Node();
        copied.put(head, temp);
        temp.val = head.val;
        temp.next = copyRandomList(head.next);
        temp.random = copyRandomList(head.random);
        return temp;
    }
}
CATALOG
  1. 1. 题意