/**
* Definition for a Node.
* type Node struct {
* Val int
* Next *Node
* Random *Node
* }
*/
func copyRandomList(head *Node) *Node {
resNode:=new(Node)
resHead:=resNode
cacheNode:=map[*Node]*Node{}
for item:=head;item!=nil;item=item.Next{
tmp:=buildNode(item.Val)
if exist,ok:=cacheNode[item];ok{
tmp=exist
}
cacheNode[item]=tmp
if item.Random!=nil{
if exist,ok:=cacheNode[item.Random];ok{
tmp.Random=exist
}else{
tmp.Random=buildNode(item.Random.Val)
cacheNode[item.Random]=tmp.Random
}
}
resHead.Next=tmp
resHead=resHead.Next
}
return resNode.Next
}
func buildNode(val int)*Node{
return &Node{
Val: val,
}
}