leetcode解题方案--023--Merge k Sorted Lists

来源:互联网 发布:php面试官常问的问题 编辑:程序博客网 时间:2024/05/19 12:29

题目

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

分析

解法一:暴力
创建数组 记录每个链表的当前结点,每次从所有链表的当前节点中选择最大的一个。并将这个链表向后移动。直到所有链表都到达末尾。
O(N\log N)O(NlogN) where NN is the total number of nodes.

public static ListNode mergeKLists(ListNode[] lists) {        ListNode[] p = new ListNode[lists.length];        ListNode header = new ListNode(0);        ListNode ans = header;        for (int i = 0; i < lists.length; i++) {            p[i] = lists[i];        }        while (true) {            int minIndex = 0, min = Integer.MAX_VALUE;            int num = lists.length;            for (int i = 0; i < p.length; i++) {                if (p[i] == null) {                    num--;                    continue;                }                if (p[i].val < min) {                    min = p[i].val;                    minIndex = i;                }            }            if (num == 0) {                break;            }            System.out.println("" + min + " " + minIndex);            header.next = p[minIndex];            p[minIndex] = p[minIndex].next;            header = header.next;        }        return ans.next;    }

解法二 优先队列法
用优先队列代替遍历整个数组寻找最大值。
总时间复杂度为O(nlogk)

//优先队列法    public static ListNode mergeKLists1(ListNode[] lists) {        ListNode dummy = new ListNode(-1);        ListNode node = dummy;        PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(new Comparator<ListNode>() {            public int compare(ListNode l1, ListNode l2) {                return l1.val - l2.val;            }        });        for (ListNode head : lists) {            if (head != null) {                pq.offer(head);            }        }        while (pq.size() > 0) {            node.next = pq.poll();            node = node.next;            if (node.next != null) {                pq.offer(node.next);            }        }        return dummy.next;    }

解法三:归并排序

自顶向下,先递归的对链表的前半部分和后半部分进行归并排序,最后再merge。
以下代码顺利AC了,时间复杂度为:O(NlogK)

public static ListNode mergeKLists2(ListNode[] lists, int left, int right) {        ListNode n1;        ListNode n2;        if (right - left > 1) {            n1 = mergeKLists2(lists, left,left+(right-left)/2);            n2 = mergeKLists2(lists,left+(right-left)/2+1,right);        } else if(right-left==0){            n1 = null;            n2 = lists[right];        } else {            n1 = lists[left];            n2 = lists[right];        }        ListNode ans = mergeK2Lists2(n1, n2);        return ans;    }    public static ListNode mergeK2Lists2(ListNode l1, ListNode l2) {        ListNode header = new ListNode(-1);        ListNode ans = header;        while (l1 != null && l2 != null) {            if (l1.val > l2.val) {                header.next = l2;                l2 = l2.next;            } else {                header.next = l1;                l1 = l1.next;            }            header = header.next;        }        if (l1 == null) {            header.next = l2;        } else {            header.next = l1;        }        return ans.next;    }
原创粉丝点击