public RandomListNode copyRandomList(RandomListNode head) {
  if (head == null) return null;
  //注意看type
  Map<RandomListNode, RandomListNode> map = new HashMap<>();
//copy all the nodes
  RandomListNode node = head;
  while (node != null) {
    map.put(node, new RandomListNode(node.label));//Pay Attention!!有new
    node = node.next;
  }
// assign next and random pointers
  node = head;
  while (node != null) {
    map.get(node).next = map.get(node.next);
    map.get(node).random = map.get(node.random);
    node = node.next;
  }
 return map.get(head);
}

results matching ""

    No results matching ""