Advertisement
k0ske

combine linked lists and sort

Nov 7th, 2022
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. template <typename T>
  2. LList<T> combineLists(const LList<T>& sorted, const LList<T>& toAdd) {
  3.     LList<T> result = sorted;
  4.     for (size_t i = 0; i < toAdd.size(); ++i) {
  5.         size_t index = 0;
  6.  
  7.         if (toAdd[i] <= result[0]) {
  8.             result.push(toAdd[i]);
  9.             continue;
  10.         }
  11.  
  12.         for (size_t j = 1; j < result.size(); j++) {
  13.             size_t prev = j - 1;
  14.             if (result[j] >= toAdd[i] && result[prev] <= toAdd[i]) {
  15.                 index = j;
  16.                 break;
  17.             }
  18.         }
  19.  
  20.         result.insertAt(index, toAdd[i]);
  21.  
  22.     }
  23.  
  24.     return result;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement