题解 | #复杂链表的复制#
复杂链表的复制
http://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba
function RandomListNode(x){
this.label = x;
this.next = null;
this.random = null;
}
function Clone(pHead)
{
// write code here
if (!pHead) return null;
let result = new RandomListNode(pHead.label);
let t = result;
let p =pHead;
p.match = result;
p= p.next;
while(p){
let node = new RandomListNode(p.label);
t.next = node;
t = node;
p.match = node;
p= p.next;
}
t.next = null;
p = pHead;
while(p){
if(p.random){
p.match.random = p.random.match;
}
p = p.next
}
return result
}
module.exports = {
Clone : Clone
};