題目:
輸入一個復(fù)雜鏈表(每個節(jié)點(diǎn)中有節(jié)點(diǎn)值,以及兩個指針,一個指向下一個節(jié)點(diǎn),另一個特殊指針random指向一個隨機(jī)節(jié)點(diǎn)),請對此鏈表進(jìn)行深拷貝,并返回拷貝后的頭結(jié)點(diǎn)。(注意,輸出結(jié)果中請不要返回參數(shù)中的節(jié)點(diǎn)引用,否則判題程序會直接返回空)
我看了幾個思路分析,比較多的都是按3步來走的,還有一種是利用遞歸的方式來解決。我覺得遞歸的方式比較簡單一些,就寫了遞歸的。
/*function RandomListNode(x){
this.label = x;
this.next = null;
this.random = null;
}*/
function Clone(pHead)
{
// write code here
if (!pHead){
return null
}
const newHead = pHead
newHead.random = pHead.random
newHead.next = Clone(pHead.next)
return newHead
}