剑指offer 复杂链表的复制

来源:互联网 发布:怎么重新注册知乎 编辑:程序博客网 时间:2024/06/07 18:14
class Solution {public:RandomListNode* Clone(RandomListNode* pHead){map<RandomListNode*, RandomListNode*> aux;RandomListNode* cur = new RandomListNode(pHead->label);RandomListNode* head = cur;RandomListNode* tem = pHead;while (tem) {tem = tem->next;if (tem) {RandomListNode* next = new RandomListNode(tem->label);aux.insert({ tem,next });cur->next = next;cur = cur->next;}}tem = head;while (tem) {tem->random = aux[pHead->random];tem = tem->next;pHead = pHead->next;}return head;}};

0 0
原创粉丝点击