分治-leetcode23. Merge k Sorted Lists

一、问题描述
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6

二、解决思路
思路一:直接通过循环,每次求出链表数组中最小节点直到结束
思路二:对于合并数组元素,可以考虑到使用分治思想,先两两分出来,然后再治理合并
思路三:参考solution思想,自己实现一个比较器,并使用优先队列(能利用现有框架,否则就要自己造轮子)构建最小堆来实现

三、算法实现
思路一

public ListNode mergeKLists(ListNode[] lists) {
        // 哨兵节点
        ListNode head = new ListNode(0);
        ListNode pre = head;
        while(!checkNull(lists)){
            ListNode n1 = check3Null(lists);
            if(n1 == null){
                if(get4Null(lists) != lists.length){
                    ListNode n2 = getListNode(lists);
                    //System.out.println("ts = " + n2.val);
                    head.next = n2;
                    head = head.next;
                }
            } else {
                head.next = n1;
                break;
            }
        }
        return pre.next;
    }
    public ListNode getListNode(ListNode[] lists){
        ListNode node = null;
        int idx = 0;
        for(int i = 0; i < lists.length; i++){
            ListNode n = lists[i];
            if(n != null){
                if(node == null){
                    node = n;
                    idx = i;
                } else {
                    if(n.val < node.val){
                        node = n;
                        idx = i;
                        //n = n.next;
                    }
                }
            }
        }
        ListNode res = node;
        ListNode tmp = lists[idx];
        lists[idx] = tmp.next;
        return res;
    }
    public int get4Null(ListNode[] lists){
        int sum = 0;
        for(ListNode node : lists){
            if(node == null){
                sum++;
            }
        }
        return sum;
    }
    public ListNode check3Null(ListNode[] lists){
        int sum = 0;
        int idx = 0;
        for(int i = 0; i < lists.length; i++){
            ListNode node = lists[i];
            if(node == null){
                sum++;
            } else {
                idx = i;
            }
        }
        if(sum == lists.length - 1){
            return lists[idx];
        }
        return null;
    }
    public boolean checkNull(ListNode[] list){
        for(ListNode node : list){
            if(node != null){
                return false;
            }
        }
        return true;
    }

思路二

public ListNode mergeKLists(ListNode[] lists) {
        if(lists.length == 1) return lists[0];
        if(lists.length == 0) return null;
        while(lists.length > 1){
            int lens = lists.length;
            ListNode[] arr = null;
            if((lens & 1) == 1){
                arr = new ListNode[lens / 2 + 1];
                int idx = 0;
                for(int i = 0; i < (lens - 1) / 2; i++){
                    ListNode p = mergeTwoLists(lists[2 * i], lists[2 * i + 1]);
                    arr[idx++] = p;
                }
                arr[idx] = lists[lens - 1];
                lists = arr;
            } else {
                arr = new ListNode[lens / 2];
                int idx = 0;
                for(int i = 0; i < lens / 2; i++){
                    ListNode p = mergeTwoLists(lists[2 * i], lists[2 * i + 1]);
                    arr[idx++] = p;
                }
                lists = arr;
            }
        }
        return lists[0];
    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null) return l2;
        if(l2 == null) return l1;
        ListNode head = null;
        if(l1.val < l2.val){
            head = l1;
            head.next = mergeTwoLists(l1.next, l2);
        } else {
            head = l2;
            head.next = mergeTwoLists(l1, l2.next);
        }
        return head;
    }

思路三

Comparator<ListNode> cmp = new Comparator<ListNode>() {
            @Override
            public int compare(ListNode o1, ListNode o2) {
                return o1.val - o2.val;
            }
        };
        ListNode head = new ListNode(0);
        ListNode pre = head;
        Queue<ListNode> queue = new PriorityQueue<>(cmp);
        for(ListNode node : lists){
            if(node != null){
                queue.add(node);
            }
        }
        while(!queue.isEmpty()){
            ListNode node = queue.poll();
            head.next = node;
            head = head.next;
            if(node != null){
                if(node.next != null){
                    queue.add(node.next);
                }
            }
        }
        return pre.next;
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容