Jommiey

Dojo1_2

Nov 15th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. List deep_copy(List& original)
  2. {
  3.   Node* current_original{original.first};
  4.   List copy{};
  5.   copy.first = new Node{current_original->value};
  6.   Node* to = copy.first;
  7.  
  8.   while (current_original->next != nullptr) {
  9.     current_original = current_original->next;
  10.     to->next = new Node{current_original->value};
  11.     to = to->next;
  12.   }
  13.  
  14.   return copy;
  15. }
  16.  
  17. /**
  18. *1. sätt current till original.first
  19. *2. kopiera det current pekar på, och lägg i copy.first
  20. *3. skapa to som pekar på det som l2.first pekar på
  21. *4. Flytta current till nästa element
  22. *5. kopiera det som current pekar på, och sätt to->next till kopian och sätt to = to->next
  23. *6. gå till steg 4 om current->next != nullptr
  24. */
Advertisement
Add Comment
Please, Sign In to add comment