Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * struct RandomListNode {
- * int label;
- * RandomListNode *next, *random;
- * RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
- * };
- */
- RandomListNode* Solution::copyRandomList(RandomListNode* A) {
- RandomListNode* temp=A;
- RandomListNode* temp2=NULL;
- RandomListNode* temp3=NULL;
- if(A->next==NULL) return temp;
- while(temp!=NULL)
- {
- temp2=temp->next;
- temp->next=new RandomListNode(temp->label);
- temp->next->next=temp2;
- temp=temp2;
- }//completed duplication
- temp=A;
- temp2=A->next;
- temp3=temp2;
- while(temp!=NULL&&temp2!=NULL)
- {
- temp2->random=temp->random->next;
- temp=temp2->next;
- temp2=temp->next;
- }//completed random part
- temp=A;
- temp2=A->next;
- while(temp!=NULL||temp2!=NULL)
- {
- temp->next=temp->next->next;
- temp2->next=temp2->next->next;
- temp=temp->next;
- temp2=temp2->next;
- }//completed seperation of lists part
- /* */
- return temp3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement