phanindhar1

Untitled

Mar 7th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1.     /*
  2.      * Complete the function below.
  3.      */
  4.     /*
  5.     For your reference:
  6.  
  7.     LinkedListNode {
  8.         int val;
  9.         LinkedListNode next;
  10.     };
  11.     */
  12.  
  13.     static LinkedListNode sort(int k, LinkedListNode head) {
  14.         if (head == null)
  15.             return null;
  16.  
  17.         LinkedListNode current = head;
  18.         LinkedListNode next = null;
  19.         int count = 1;
  20.  
  21.         while (current != null && count < k) {
  22.             current = current.next;
  23.             count++;
  24.         }
  25.  
  26.         if (current != null) {
  27.             next = current.next;
  28.             current.next = null;
  29.         } else
  30.             next = null;
  31.  
  32.         //printList(head);
  33.         head = mergeSort(head);
  34.  
  35.         LinkedListNode end = head;
  36.         //printList(head);
  37.         while (end != null && end.next != null) {
  38.             end = end.next;
  39.         }
  40.  
  41.         if (end != null)
  42.             end.next = sort(k, next);
  43.  
  44.         return head;
  45.     }
  46.  
  47.     static LinkedListNode sortedMerge(LinkedListNode a, LinkedListNode b) {
  48.         LinkedListNode result = null;
  49.         /* Base cases */
  50.         if (a == null)
  51.             return b;
  52.         if (b == null)
  53.             return a;
  54.  
  55.         /* Pick either a or b, and recur */
  56.         if (a.val <= b.val) {
  57.             result = a;
  58.             result.next = sortedMerge(a.next, b);
  59.         } else {
  60.             result = b;
  61.             result.next = sortedMerge(a, b.next);
  62.         }
  63.         return result;
  64.  
  65.     }
  66.  
  67.     static LinkedListNode mergeSort(LinkedListNode h) {
  68.         // Base case : if head is null
  69.         if (h == null || h.next == null) {
  70.             return h;
  71.         }
  72.  
  73.         // get the middle of the list
  74.         LinkedListNode middle = getMiddle(h);
  75.         LinkedListNode nextofmiddle = middle.next;
  76.  
  77.         // set the next of middle node to null
  78.         middle.next = null;
  79.  
  80.         // Apply mergeSort on left list
  81.         LinkedListNode left = mergeSort(h);
  82.  
  83.         // Apply mergeSort on right list
  84.         LinkedListNode right = mergeSort(nextofmiddle);
  85.  
  86.         // Merge the left and right lists
  87.         LinkedListNode sortedlist = sortedMerge(left, right);
  88.         return sortedlist;
  89.     }
  90.  
  91.     // Utility function to get the middle of the linked list
  92.     static LinkedListNode getMiddle(LinkedListNode h) {
  93.         //Base case
  94.         if (h == null)
  95.             return h;
  96.         LinkedListNode two = h.next;
  97.         LinkedListNode one = h;
  98.  
  99.         // Move fastptr by two and slow ptr by one
  100.         // Finally slowptr will point to middle node
  101.         while (two != null) {
  102.             two = two.next;
  103.             if (two != null) {
  104.                 one = one.next;
  105.                 two = two.next;
  106.             }
  107.         }
  108.         return one;
  109.     }
  110.  
  111.     private static long ncr(int n, int k) {
  112.         if (n == 0) return 1;
  113.         if (k < n - k) k = n - k;
  114.         long ans = 1;
  115.  
  116.         for (int i = 1; i <= k; i++) {
  117.             ans *= (n - i + 1);
  118.             ans /= i;
  119.         }
  120.         return ans;
  121.     }
Add Comment
Please, Sign In to add comment