标签:输入 with copy 复杂 str ret 技术分享 复制 link
1 /** 2 * Definition for singly-linked list with a random pointer. 3 * struct RandomListNode { 4 * int label; 5 * RandomListNode *next, *random; 6 * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 RandomListNode *copyRandomList(RandomListNode *head) { 12 RandomListNode *newList = NULL; 13 RandomListNode *newHead = NULL; 14 RandomListNode *p = head; 15 16 if(head==NULL) 17 return NULL; 18 //把每一个新的结点放入旧结点后面 19 while(p!=NULL) 20 { 21 RandomListNode *q = new RandomListNode(p->label); 22 q->next = p->next; 23 p->next = q; 24 25 p = q->next; 26 } 27 28 p = head; 29 //在新结点中加入random 30 while(p!=NULL) 31 { 32 if(p->random != NULL) 33 p->next->random = p->random->next; 34 p = p->next->next;//此时p需要跳两步才到旧的下一节点 35 } 36 p = head; 37 //注意这里的条件,首先要判断p是否为null 38 while(p!=NULL && p->next!=NULL) 39 { 40 if(p==head) 41 { 42 newHead = p->next; 43 newList = newHead; 44 } 45 else 46 { 47 newList->next = p->next; 48 newList = newList->next; 49 } 50 51 52 p->next = newList->next; 53 p = p->next; 54 //如果使用p = newList->next;替换以上两步,则会改变原链表,错误 55 56 } 57 58 return newHead;//返回值是newHead而不是newList,因为此时newList指向尾部 59 } 60 };
标签:输入 with copy 复杂 str ret 技术分享 复制 link
原文地址:http://www.cnblogs.com/qqky/p/6915053.html