題目描述
輸入一個復雜鏈表(每個節點中有節點值,以及兩個指標,一個指向下一個節點,另一個特殊指標指向任意一個節點),回傳結果為復制后復雜鏈表的head,(注意,輸出結果中請不要回傳引數中的節點參考,否則判題程式會直接回傳空)
思路

牛客網鏈接
js代碼
/*function RandomListNode(x){
this.label = x;
this.next = null;
this.random = null;
}*/
function Clone(pHead)
{
// write code here
if (!pHead) return null
let p = pHead
while (p) {
let node = new RandomListNode(p.label)
node.next = p.next
p.next = node
p = p.next.next
}
p = pHead
while (p) {
if (p.random) p.next.random = p.random
else p.next.random = null
p = p.next.next
}
p = pHead.next
let root = new RandomListNode(0)
let q = root
while (p) {
q.next = p
q = q.next
if (p.next) p = p.next.next
else break
}
q.next = null
return root.next
}```
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/139442.html
標籤:其他
上一篇:from matplotlib.pyplot as plt出錯
下一篇:數論篇1——素數問題
