Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. /*
  2. struct RandomListNode {
  3. int label;
  4. struct RandomListNode *next, *random;
  5. RandomListNode(int x) :
  6. label(x), next(NULL), random(NULL) {
  7. }
  8. };
  9. */
  10. class Solution {
  11. private:
  12. RandomListNode* push_back(RandomListNode* pre,RandomListNode* cur){
  13. RandomListNode* node=new RandomListNode(cur->label);
  14. pre->next=node;
  15. return node;
  16. }
  17. public:
  18. RandomListNode* Clone(RandomListNode* pHead)
  19. {
  20. RandomListNode* cur=pHead;
  21. RandomListNode* head=new RandomListNode(0);
  22. RandomListNode* node=head;
  23. unordered_map<RandomListNode*,RandomListNode*> record;
  24. while(cur){
  25. node=push_back(node,cur);
  26. record[cur]=cur->random;
  27. cur=cur->next;
  28. }
  29. cur=head->next;
  30. while(cur){
  31. RandomListNode* tmp=head->next;
  32. if(record.find(cur)!=record.end()){
  33. while(tmp){
  34. if(tmp==record[cur]){
  35. tmp->random=cur->random;
  36. }
  37. tmp=tmp->next;
  38. }
  39. }
  40. cur=cur->next;
  41. }
  42. return head->next;
  43. }
  44. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement