题目描述:
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
这是一个单纯的哈希表问题, 不知道为什么归类为hard. 首先遍历一次链表进行拷贝, 同时建立原链表节点到新链表节点的映射关系, 再遍历一次新链表更新random的值即可.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
class Solution { public: RandomListNode *copyRandomList(RandomListNode *head) { unordered_map<RandomListNode*, RandomListNode*> m; if(!head) return nullptr; RandomListNode *p = head, *newHead = new RandomListNode(head->label), *np = newHead; while(p){ if(p->next) np->next = new RandomListNode(p->next->label); np->random = p->random; m[p] = np; p = p->next; np = np->next; } p = newHead; while(p){ if(p->random){ p->random = m[p->random]; } p = p->next; } return newHead; } };
|