zoukankan      html  css  js  c++  java
  • Leetcode-Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

    Have you met this question in a real interview?
     
    Analysis:
    record the current head of each list. Use binary insertation to arrange the head list, select the minimum one and add to the end of the return list.
     
    Newest Solution (Use PriorityQueue):
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode mergeKLists(ListNode[] lists) {
            if (lists.length==0) return null;
            
            ListNode preHead = new ListNode(0);
            ListNode end = preHead;
            PriorityQueue<ListNode> queue = new PriorityQueue<ListNode>((a,b) -> (a.val-b.val));
            for (ListNode node : lists)
                if (node!=null){
                    queue.add(node);
                }
            
            while (!queue.isEmpty()){
                ListNode nextNode = queue.poll();
                end.next = nextNode;
                if (nextNode.next!=null){
                    queue.add(nextNode.next);
                }
                end = nextNode;
            }
            
            return preHead.next;
        }
    }
     
     
    Solution:
     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) {
     7  *         val = x;
     8  *         next = null;
     9  *     }
    10  * }
    11  */
    12 public class Solution {
    13     public ListNode mergeKLists(List<ListNode> lists) {
    14         //Consider the cases where the element in lists is NULL!
    15         for (int i=0;i<lists.size();i++)
    16             if (lists.get(i)==null){
    17                 lists.remove(i);
    18                 i--;
    19             }
    20         if (lists.size()==0) return null;
    21         if (lists.size()==1) return lists.get(0);
    22 
    23         ListNode preHead = new ListNode(0);
    24         ListNode end = preHead;
    25         List<ListNode> curList = new ArrayList<ListNode>();
    26         curList.add(lists.get(0));
    27         for (int i=1;i<lists.size();i++) binaryInsert(curList,lists.get(i));
    28         while (curList.size()!=0){
    29             if (curList.size()==1){
    30                 end.next = curList.get(0);
    31                 end = new ListNode(0);
    32                 break;
    33             }
    34             ListNode target = curList.get(0);            
    35             curList.remove(0);
    36             if (target.next!=null) binaryInsert(curList,target.next);
    37             end.next = target;
    38             end = target;
    39         }
    40         end.next = null;
    41         return preHead.next;
    42         
    43     }
    44 
    45     public void binaryInsert(List<ListNode> lists, ListNode node){
    46         int start = 0;
    47         int end = lists.size()-1;
    48         int index = -1;
    49         while (start<end){
    50             int mid = (start+end)/2;
    51             if (node.val == lists.get(mid).val){
    52                 index = mid;
    53                 break;
    54             }
    55 
    56             if (node.val>lists.get(mid).val){
    57                 start = mid+1;
    58                 continue;
    59             }
    60 
    61             if (node.val<lists.get(mid).val){
    62                 end = mid-1;
    63                 continue;
    64             }
    65         }
    66         
    67         //NOTE: There are two ending cases, we need consider them carefully!.
    68         if (index==-1){
    69             if (start==end)
    70                 if (node.val>lists.get(start).val) index=start+1;
    71                 else index = start;
    72             else index = start;   //i.e., start>end.
    73         }  
    74 
    75         lists.add(index,node);
    76 
    77         return;
    78     }        
    79 }
  • 相关阅读:
    多项式大合集
    【题解】Codeforces 961G Partitions
    【题解】Counting D-sets(容斥+欧拉定理)
    【题解】分特产(组合数+容斥)
    【题解】P4247 [清华集训]序列操作(线段树修改DP)
    【题解】没有上司的舞会
    【题解】数字组合(NTT+组合 滑稽)
    【瞎总结】组合模型及其组合意义的阐释
    P2822 组合数问题——巧用前缀和
    P3239 [HNOI2015]亚瑟王——概率DP
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4114641.html
Copyright © 2011-2022 走看看