这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » copy-list-with-random-pointer

共1条 1/1 1 跳转至

copy-list-with-random-pointer

高工
2018-01-31 13:11:21     打赏

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.


复杂链表的拷贝;.


[cpp] view plain copy
  1. class Solution  

  2. {  

  3. public:  

  4.     RandomListNode* copyRandomList(RandomListNode* head)  

  5.     {  

  6.         RandomListNode* copy, *p;  

  7.         if (!head)  

  8.         {  

  9.             return NULL;  

  10.         }  

  11.   

  12.         for (p=head; p; )  

  13.         {  

  14.             copy = new RandomListNode(p->label);  

  15.             copy->next = p->next;  

  16.             p = p->next = copy;  

  17.             p = p->next;  

  18.         }  

  19.   

  20.         for (p=head;p; )  

  21.         {  

  22.             copy = p->next;  

  23.             copy->random = (p->random ? p->random->next:NULL);  

  24.             p = copy->next;  

  25.         }  

  26.   

  27.         for (p=head, head=copy=p->next; p;)  

  28.         {  

  29.             p = p->next = copy->next;  

  30.             copy = copy->next = (p?p->next:NULL);  

  31.         }  

  32.         return head;  

  33.     };  




共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]