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;
};