Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. void Merge2Lists(Node* list1, Node* list2, Node* result)
  2. {
  3. Node* current = NULL;
  4. Node* end_of_result = result;
  5.  
  6. while (list1 != NULL && list2 != NULL)
  7. {
  8. OP += 2;
  9. if (list1->data <= list2->data)
  10. {
  11. // Creating a new node that will store the data of list1
  12. current = (Node*)malloc(sizeof(Node));
  13. current->data = list1->data;
  14. current->next = NULL;
  15.  
  16. // Going to the next element
  17. list1 = list1->next;
  18.  
  19. end_of_result->next = current;
  20. OP += 4;
  21. }
  22. else
  23. {
  24. // Creating a new node that will store the data of list2
  25. current = (Node*)malloc(sizeof(Node));
  26. current->data = list2->data;
  27. current->next = NULL;
  28.  
  29. // Going to the next element
  30. list2 = list2->next;
  31.  
  32. end_of_result->next = current;
  33. OP += 4;
  34. }
  35. end_of_result = end_of_result->next;
  36. OP += 2;
  37. }
  38. if (list1 == NULL || list2 == NULL) OP += 2;
  39.  
  40. if (list1 == NULL)
  41. end_of_result->next = list2;
  42. else
  43. end_of_result->next = list1;
  44. OP++;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement