Advertisement
1988coder

23. Merge k Sorted Lists

Jan 7th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/add-two-numbers/
  3. import java.util.PriorityQueue;
  4.  
  5. //   Definition for singly-linked list.
  6. class ListNode {
  7.     int val;
  8.     ListNode next;
  9.  
  10.     ListNode(int x) {
  11.         val = x;
  12.     }
  13. }
  14.  
  15. /**
  16.  * Using Priority Queue
  17.  *
  18.  * Time Complexity: O(N log K)
  19.  *
  20.  * Space Complexity: O(K)
  21.  *
  22.  * N = Total number of all nodes in all lists. K = Number of lists.
  23.  */
  24. class Solution1 {
  25.     public ListNode mergeKLists(ListNode[] lists) {
  26.         if (lists == null || lists.length == 0) {
  27.             return null;
  28.         }
  29.         if (lists.length == 1) {
  30.             return lists[0];
  31.         }
  32.  
  33.         PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, (a, b) -> (a.val - b.val));
  34.  
  35.         ListNode dummyNode = new ListNode(-1);
  36.         ListNode prevNode = dummyNode;
  37.  
  38.         for (ListNode listHead : lists) {
  39.             if (listHead != null) {
  40.                 queue.offer(listHead);
  41.             }
  42.         }
  43.  
  44.         while (!queue.isEmpty()) {
  45.             prevNode.next = queue.poll();
  46.             prevNode = prevNode.next;
  47.  
  48.             if (prevNode.next != null) {
  49.                 queue.offer(prevNode.next);
  50.             }
  51.         }
  52.  
  53.         return dummyNode.next;
  54.     }
  55. }
  56.  
  57. /**
  58.  * Merge with Divide & Conquer
  59.  *
  60.  * Time Complexity: Divide and Merge operation forms a tree with height log K.
  61.  * Suppose there are N total nodes in all the list.. Thus the total time
  62.  * complexity is O(N * log K)
  63.  *
  64.  * Space Complexity: O(1)
  65.  *
  66.  * N = Total number of all nodes in all lists. K = Number of lists.
  67.  */
  68. class Solution2 {
  69.     public ListNode mergeKLists(ListNode[] lists) {
  70.         if (lists == null || lists.length == 0) {
  71.             return null;
  72.         }
  73.         if (lists.length == 1) {
  74.             return lists[0];
  75.         }
  76.  
  77.         int interval = 1; // Interval to find the index of lists to be merged.
  78.         while (interval < lists.length) {
  79.             for (int i = 0; i < lists.length - interval; i += interval * 2) {
  80.                 lists[i] = merge2Lists(lists[i], lists[i + interval]);
  81.             }
  82.             interval *= 2;
  83.         }
  84.  
  85.         return lists[0];
  86.     }
  87.  
  88.     private ListNode merge2Lists(ListNode l1, ListNode l2) {
  89.         if (l1 == null) {
  90.             return l2;
  91.         }
  92.         if (l2 == null) {
  93.             return l1;
  94.         }
  95.  
  96.         ListNode dummyHead = new ListNode(-1);
  97.         ListNode prevNode = dummyHead;
  98.         ListNode p = l1;
  99.         ListNode q = l2;
  100.  
  101.         while (p != null && q != null) {
  102.             if (p.val <= q.val) {
  103.                 prevNode.next = p;
  104.                 p = p.next;
  105.             } else {
  106.                 prevNode.next = q;
  107.                 q = q.next;
  108.             }
  109.             prevNode = prevNode.next;
  110.             prevNode.next = null;
  111.         }
  112.  
  113.         prevNode.next = p == null ? q : p;
  114.         return dummyHead.next;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement