# 15.复制含有随机指针的链表

### 【代码1】

class Solution {
public Node copyRandomList(Node head) {
Node cur = head;
Map<Node, Node> map = new HashMap<Node, Node>();
while(cur != null){
map.put(cur, new Node(cur.val));
cur = cur.next;
}
while(cur != null){
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.next;
}
}
}


###【代码2】

class Solution {
public Node copyRandomList(Node head) {
return null;
}
Node random = null;
//1.每一个真实的节点后面加入一个拷贝节点
Node node = head;
while(node != null){
Node copyNode = new Node(node.val);
Node nextNode = node.next;
node.next = copyNode;
copyNode.next = nextNode;
node = nextNode;
}

//2.copy Random节点
while(node != null){
Node nextNode = node.next.next;
Node nodeRandom = node.random;
if(nodeRandom == null){
node.next.random = null;
}else{
node.next.random = nodeRandom.next;
}
node = nextNode;
}

random = node.next;
Node resultNode = random;
//3.获得copy节点
while(node != null){
Node nextNode = node.next.next;
Node randomNext = random.next == null ? null: random.next.next;
node.next = nextNode;
random.next = randomNext;
node = nextNode;
random = randomNext;
}
return resultNode;
}
}


10-08 159

02-04 73

08-14 69

06-17 146

10-09 767

03-09 142

05-15 35

04-18 595

05-14 60

#### 十三.复制含有随机指针节点的链表

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。