leetcode 21. Merge Two Sorted Lists(23. Merge k Sorted Lists)

21. Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

 

按照某种规则合并两种列表的解法:

常规解法:定义一个假头,之后的内容为结果。按从前至后顺序完成归并排序操作。

 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode fakeNode= new ListNode(-1),cur= fakeNode;
        while(l1!=null || l2!=null){
            if(l1==null){cur.next=l2;  break;}
            if(l2==null){cur.next=l1;   break;}
            if(l1.val< l2.val){
                cur.next= l1;
                l1=l1.next;
            }
            else{
                cur.next=l2;
                l2=l2.next;
            }
            cur=cur.next;
        }
        return fakeNode.next;
    }

递归解法:(该方法栈空间消耗大,不适合大数量数据集)

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

23. 

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

优先队列解法:(需自行构造比较器)

public ListNode mergeKLists(ListNode[] lists) {
        ListNode fakeHead= new ListNode(-1), cur= fakeHead;
        if(lists==null || lists.length==0)   return fakeHead.next;
        PriorityQueue<ListNode> sortQueue= new PriorityQueue<>(valComparator);
        for(ListNode l: lists){
            if(l!=null)
                sortQueue.offer(l);
        }
        while(sortQueue.size()!= 0){
            cur.next= sortQueue.poll();
            if(cur.next.next!=null)
                sortQueue.offer(cur.next.next);
            cur= cur.next;
        }
        return fakeHead.next;
    }
    public static Comparator<ListNode> valComparator= new Comparator<ListNode>(){
        public int compare(ListNode l1, ListNode l2){
            return l1.val-l2.val;
        }
    };

divide and conquer解法:

由于是将k个链表按顺序合成一个,可将k个链表对半分开,直至分的只剩一个或两个为止,再合并。与归并排序的思路是一样的

public ListNode mergeKLists(ListNode[] lists) {
        ListNode res= new ListNode(-1);
        if(lists==null || lists.length==0)  return res.next;
        res.next= mergeListsCore(lists,0,lists.length-1);
        return res.next;
    }
    public ListNode mergeListsCore(ListNode[] lists, int start, int end){
        if(start== end)  return lists[start];
        if(start+1== end){
            return mergeTwoList(lists[start],lists[end]);
        }
        int mid= (start+end)/2;
        ListNode left= mergeListsCore(lists,start,mid);
        ListNode right= mergeListsCore(lists,mid+1,end);
        return mergeTwoList(left,right);
    }
    public ListNode mergeTwoList(ListNode l1, ListNode l2){
        ListNode fakeNode= new ListNode(-1),cur= fakeNode;
        while(l1!=null || l2!=null){
            if(l1==null){cur.next=l2;  break;}
            if(l2==null){cur.next=l1;   break;}
            if(l1.val< l2.val){
                cur.next= l1;
                l1=l1.next;
            }
            else{
                cur.next=l2;
                l2=l2.next;
            }
            cur=cur.next;
        }
        return fakeNode.next;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值