Merge k Sorted Lists

来源:互联网 发布:零基础学seo 编辑:程序博客网 时间:2024/06/06 03:34

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

总结一下数据结构:Heap 在java代码里面的实现,使用PriorityQueue, 它是一个class,是可以直接实例化的。

https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html

构造函数是一般需要size 和comparator,一般我们需要重新写一个comparator的类,来实现我们想要的sorting 顺序,递增或者递减。

PriorityQueue<ListNode> queue = new PriorityQueue<ListNode>(lists.length, new myComparator());

思路:这题用PriorityQueue来sort,每次取每个list的开头node,丢到priorityqueue里面,进行sort,priorityqueue自动sort是logk的复杂度,你可以拿到最小值,然后取出来,append到dump list,然后加入poll出来node的下一个,然后继续运行,直到queue为空,则dump list为sort好的list。

/**  * 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 == null || lists.length == 0) return null;         PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(lists.length, new Comparator<ListNode>() {            @Override            public int compare(ListNode a, ListNode b){                return (a.val - b.val);            }         });                  for(ListNode node: lists){             if(node!=null){                 pq.add(node);             }         }                  ListNode dump = new ListNode(0);         ListNode cur = dump;         while(pq.size()>0){             ListNode node = pq.poll();             if(node!=null && node.next!=null){                 pq.add(node.next);             }             cur.next = node;             cur = cur.next;         }         return dump.next;     } }

思路2:merge two sorted list, 我会做。这个题目用到了mergesort的递归思想。
把k list分为两边,然后分而治之。

    public ListNode mergeKlist(List<ListNode> lists, int start, int end){        if(start<end){            int mid = start+(end-start)/2;            return merge(mergeKlist(lists,start,mid), mergeKlist(lists,mid+1,end));        }else{            return lists.get(start); // start == 0 == end;        }    }

这个很巧妙,需要学习。

//**  * Definition for singly-linked list.  * public class ListNode {  *     int val;  *     ListNode next;  *     ListNode(int x) {  *         val = x;  *         next = null;  *     }  * }  */ public class Solution {     public ListNode mergeKLists(List<ListNode> lists) {         if(lists == null || lists.size()==0) return null;         return mergeLists(lists,0,lists.size()-1);     }          public ListNode mergeLists(List<ListNode> lists, int start, int end){         if(start<end){             int mid = start +(end-start)/2;             return merge(mergeLists(lists,start,mid),mergeLists(lists,mid+1,end));         }else{ //start = end;             return lists.get(start);         }     }     public ListNode merge(ListNode l1, ListNode l2){         ListNode dump = new ListNode(0);         ListNode cur = dump;         while(l1!=null && l2!=null){             ListNode l1next = l1.next;             ListNode l2next = l2.next;             if(l1.val < l2.val){                 cur.next = l1;                 l1.next = null;                 cur = l1;                 l1 = l1next;                             } else { // l1.val > l2.val                 cur.next = l2;                 l2.next = null;                 cur = l2;                 l2 = l2next;             }         }                  if(l1!=null){             cur.next = l1;         }         if(l2!=null){             cur.next = l2;         }         return dump.next;     }} 
                                             
0 0
原创粉丝点击