Series 19 wins the offer: Complex copy of the list

This question is typical of divide and conquer, it will decompose a large problem into several small steps to resolve. We must pay attention to whether to use the pointer when the pointer to an empty question. When pointer to be a null pointer pointing to an assignment to write, but the null pointer can not point to any place (point blank does not work), you must remember this point.

  1 #include<iostream>
  2 #include<vector>
  3 using namespace std;
  4 struct RandomListNode {
  5 int label;
  6 struct RandomListNode *next, *random;
  7 /*
  8 RandomListNode(int x) :
  9 label(x), next(NULL), random(NULL) {
 10 }
 11 */
 12 };
 13 
 14 class Solution {
 15 public:
 16     RandomListNode* Clone(RandomListNode* pHead)
 17     {
 18 is          IF (PHEAD == NULL)
 . 19              return NULL;
 20 is          // . 3 steps
 21          // first step, the replication complex as chain Double 
22 is          nodeclone (PHEAD);
 23 is          Connect (PHEAD);
 24          return Reconnect (PHEAD);
 25      }
 26 is      void nodeclone (RandomListNode * Head)
 27      {
 28          RandomListNode * P = Head;
 29          the while (! P = NULL)
 30          {
 31 is              // the insertion node
 32             // CUR = p-> Next; 
33 is              RandomListNode * = CURNODE new new RandomListNode ();
 34 is              curnode-> p-label => label;
 35              curnode-> = p-Next> Next;
 36              curnode-> Random = NULL; / / this sentence must write? 
37 [              p-> Next = CURNODE;
 38 is              P = curnode-> Next; // suspension pointer? 
39          }
 40      }
 41 is      void Connect (RandomListNode * Head)
 42 is      {
 43 is          // Step: random duplicated pointer pointing to the node in any position
44 is          RandomListNode * P = Head;
 45          the while (! P = NULL) // when the pointer accesses, always pay attention to whether the air 
46 is          {
 47              RandomListNode * = p-CURNODE> Next;
 48  
49              IF (p-> Random =! NULL)
 50              {
 51 is                  curnode-> = p-Random> random-> Next;
 52 is              }
 53 is  
54 is              P = curnode-> Next;
 55  
56 is          }
 57 is      }
 58      RandomListNode Reconnect * (* RandomListNode Head)
59      {
 60          // Third Step: split into two parts list 
61 is          RandomListNode * P = Head;
 62 is          RandomListNode * = p-Copy> Next;
 63 is          the while (P =! NULL)
 64          {
 65              RandomListNode * = p-CURNODE> Next;
 66              IF (! CURNODE = NULL)
 67              {
 68                  p-> Next = curnode-> Next;
 69              }
 70          
71 is              P = CURNODE;
 72          }
 73 is          return copy;
 74     }
 75 };
 76 int main()
 77 {
 78     RandomListNode list[5];
 79     list[0].label = 1;
 80     list[0].next = &list[1];
 81     list[0].random = &list[2];
 82 
 83     list[1].label = 2;
 84     list[1].next = &list[2];
 85     list[1].random = &list[4];
 86 
 87     list[2].label = 3;
 88     list[2].next = &list[3];
 89     list[2].random = NULL;
 90 
 91     list[3].label = 4;
 92     list[3].next = &list[4];
 93     list[3].random = &list[1];
 94 
 95     list[4].label = 5;
 96     list[4].next =NULL;
 97     list[4].random = NULL;
 98 
 99     Solution solu;
100     RandomListNode *re = solu.Clone(list);
101     int count=0;
102     while (re != NULL)
103     {
104         //cout << re->label << " "<<re->random->label<<",";
105         cout << re->label<<" ";
106         if (re->random != NULL)
107             cout << re->random->label;
108         cout << endl;
109         count++;
110         re = re->next;
111     }
112     //cout << endl;
113     cout << "number of array:"<<count << endl;
114     return 0;
115 }

Prior to writing this in another blog, now make it up.

Guess you like

Origin www.cnblogs.com/neverland0718/p/11320088.html