面試題35 複雜鍊錶的複製

2021-08-22 12:13:04 字數 760 閱讀 6027

題目描述

輸入乙個複雜鍊錶(每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點),返回結果為複製後複雜鍊錶的head。(注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空)

方法一:三步

/*

struct randomlistnode

};*/

class solution

}void connectrandomnodes(randomlistnode* phead)

}randomlistnode* reconnect(randomlistnode* phead)

return res;

}randomlistnode* clone(randomlistnode* phead)

};

方法二:

class solution 

currnode = phead;

while(currnode)

currnode = node->next;

}//拆分

randomlistnode *pclonehead = phead->next;

randomlistnode *tmp;

currnode = phead;

while(currnode->next)

return pclonehead;

}};

面試題35 複雜鍊錶的複製

一 題目 請實現函式complexlistnode clone complexlistnode phead 複製乙個複雜鍊錶。在複雜鍊錶中,每個結點除了有乙個m pnext指標指向下乙個結點外,還有乙個m psibling 指向鍊錶中的任意結點或者nullptr。二 關鍵 1.在原始鍊錶上擴充新的鍊...

面試題35 複雜鍊錶的複製

請實現 copyrandomlist 函式,複製乙個複雜鍊錶。在複雜鍊錶中,每個節點除了有乙個 next 指標指向下乙個節點,還有乙個 random 指標指向鍊錶中的任意節點或者 null。示例 1 輸入 head 7,null 13,0 11,4 10,2 1,0 輸出 7,null 13,0 1...

面試題35 複雜鍊錶的複製

題目 請實現函式complexlistnode clone complexlistnode phead 復 制乙個複雜鍊錶。在複雜鍊錶中,每個結點除了有乙個m pnext指標指向下乙個結點外,還有乙個m psibling 指向鍊錶中的任意結點或者nullptr。include include inc...