Advertisement
Jakubowiczish

Untitled

Mar 6th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. void SplitList_v3(Node *list, Node *&list1, Node *&list2)
  2. {
  3. Node *slow = list;
  4. Node *fast = list;
  5. while(fast -> next != nullptr and fast -> next -> next != nullptr){
  6. slow = slow -> next;
  7. fast = fast -> next -> next;
  8. }
  9. list1 = slow -> next;
  10. slow -> next = nullptr;
  11. list2 = list;
  12. }
  13.  
  14. Node *MergeLists_v3(Node *&list1, Node *&list2)
  15. {
  16. Node *result = nullptr;
  17. if(list1 == nullptr)
  18. return list2;
  19. else if(list2 == nullptr)
  20. return list1;
  21.  
  22. if(list1 -> val < list2 -> val){
  23. result = list1;
  24. result -> next = MergeLists_v3(list1 -> next,list2);
  25. }
  26. else{
  27. result = list2;
  28. result -> next = MergeLists_v3(list1,list2 -> next);
  29. }
  30. return result;
  31. }
  32.  
  33. void MergeSortList(Node *&list)
  34. {
  35. if(list == nullptr or list->next == nullptr) return;
  36. Node *list1, *list2;
  37. SplitList_v3(list, list1, list2);
  38. MergeSortList(list1);
  39. MergeSortList(list2);
  40. list = MergeLists_v3(list1, list2);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement