【copy-list-with-random-pointer】

来源:互联网 发布:nga178魔兽世界数据库 编辑:程序博客网 时间:2024/06/03 21:45

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.


复杂链表的拷贝;.

class Solution{public:RandomListNode* copyRandomList(RandomListNode* head){RandomListNode* copy, *p;if (!head){return NULL;}for (p=head; p; ){copy = new RandomListNode(p->label);copy->next = p->next;p = p->next = copy;p = p->next;}for (p=head;p; ){copy = p->next;copy->random = (p->random ? p->random->next:NULL);p = copy->next;}for (p=head, head=copy=p->next; p;){p = p->next = copy->next;copy = copy->next = (p?p->next:NULL);}return head;};


原创粉丝点击