23. Merge K Sorted Lists (Java, 归并排序的思路)

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

解析:合并k个已经有序的单链表,使其最终成为一个有序的单链表。原理就是归并排序,递归运算。基本算法recusion 与 merge

编码:

public ListNode mergeKLists(ListNode[] lists) {
        if(lists == null || lists.length == 0)
           return null;
        if(lists.length == 1)
           return lists[0];
        return recursion(lists,0,lists.length - 1);
    }
    //recursion
    public ListNode recursion(ListNode[] lists,int start,int end){
        if(start == end)//只有一个链表
           return lists[start];
        if(start < end){
            int mid = start + (end - start) / 2; //注意:这里防止整数越界的处理,start+(end-start)/2
            ListNode l1 = recursion(lists,start,mid);
            ListNode l2 = recursion(lists,mid + 1,end);
            return merge(l1,l2);
        } else
            return null;
        
    }
    //merge
    public ListNode merge(ListNode l1,ListNode l2){
        ListNode head = new ListNode(0); //创建一个头结点,最后还要删掉
        ListNode p = head;
        while(l1 != null && l2 != null){
            if(l1.val <= l2.val){
                p.next = l1;
                l1 = l1.next;
            } else{
                p.next = l2;
                l2 = l2.next;
            }
            p = p.next;
        }
        
        p.next = (l1 != null) ? l1 : l2;
        return head.next;// head的下一个节点是第一个数据结点
    }

 

posted @ 2016-08-13 21:23  江湖凶险  阅读(435)  评论(0编辑  收藏  举报