Vassil_Iliev

Merge sort

May 23rd, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mergeSortedLists = (h1, h2) => {
  2.     let head;
  3.     let temp;
  4.  
  5.     if (!h1)
  6.         return h2;
  7.  
  8.     if (!h2)
  9.         return h1;
  10.  
  11.     while (h1 || h2) {
  12.         if (!h2) {
  13.             temp.next = new ListNode(h1.value, null);
  14.             temp = temp.next;
  15.             h1 = h1.next;
  16.  
  17.         } else if (!h1) {
  18.             temp.next = new ListNode(h2.value, null);
  19.             temp = temp.next;
  20.             h2 = h2.next;
  21.  
  22.         } else if (!head && h1.value >= h2.value) {
  23.             head = new ListNode(h2.value, null);
  24.             temp = head;
  25.             h2 = h2.next;
  26.  
  27.         } else if (head && h1.value >= h2.value) {
  28.             temp.next = new ListNode(h2.value, null);
  29.             temp = temp.next
  30.             h2 = h2.next;
  31.  
  32.         } else if (!head && h1.value <= h2.value) {
  33.             head = new ListNode(h1.value, null);
  34.             temp = head;
  35.             h1 = h1.next;
  36.  
  37.         } else if (head && h1.value <= h2.value) {
  38.             temp.next = new ListNode(h1.value, null);
  39.             temp = temp.next
  40.             h1 = h1.next;
  41.         }
  42.     }
  43.  
  44.     return head;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment