Merge k Sorted Lists Java

Two Approach:

/*Solution1: 
* KeyWord: MergeSort,Iteration 
* Approach:
* 1. Divide K linked list into k/2 linked list
* 2. Merge two separated each time iteratively.
* example of k=6;
* => merge 1 and 4; save on 1
* => merge 2 and 5; save on 2
* => merge 3 and 6; save on 3
* then merge 1 and 2 (as a result: merge of 14 and 25); save on 1
* => merge 1 and 3 (as a result: merge of 1425 and 36); save on 1
* return 1;
* Time analysis: 
* T(k)= 2T(k/2) + O(n*k)
* in total:=> O(k*n*logk) 
* Space Complexity: O(logk) not including space cost during recursive 
*/

public ListNode mergeKListsMerge(List<ListNode> lists) {
		int k=lists.size();
		if(k==0) return null;
		else{
			while(k >1)
	        {
	            int  mid= (k+1)/2;
	            for(int i = 0; i < k/2; i++){
	                lists.set(i, mergeTwoLists(lists.get(i), lists.get(i + mid)));
	            }
	            k = mid;
	        }
	        return lists.get(0);
		} 
	}
	 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
	        //check for base case
	        if(l1==null) return l2;
	        if(l2==null) return l1;
	        if(l1==null && l2==null) return l1;

	        //delcare a dummy node
	        ListNode dummy=new ListNode(0);
	        //delcare a temple ListNode ll
	        ListNode ll=dummy;

	        while(l1!=null && l2!=null){
	        	if(l1.val<l2.val){
	        		ll.next=l1;
	        		l1=l1.next;
	        	}else{
	        		ll.next=l2;
	        		l2=l2.next;
	        	}

	        	ll=ll.next;        	
	        }
	        if(l1!=null){
	        	ll.next=l1;
	        }else{
	        	ll.next=l2;
	        }
	        return dummy.next;
	    }

/*Solution2: 
* KeyWord: PriorityQueue
* Approach: 
* it require to sort input lists, and merge together, then output one.
* 1. Use the DataStructure of PriorityQueue that
* 2. The idea in behind is to always keep smallest element in the top of heap. 
* Time analysis: since it require go over each element once => O(k*n),
* insert operation in PriorityQueue => O(logk)
* k is size of PriorityQueue Time complexity in total:=> O(k*n*logk) 
* Space Complexity: O(k) size of PriorityQueue
*/

public ListNode mergeKLists(List<ListNode> lists) {
		if(lists.size()==0) return null;
		// define a ListNode Comparator
		Comparator<ListNode> compListNode = new Comparator<ListNode>() {
			public int compare(ListNode a, ListNode b) {
				if (a.val > b.val)
					return 1;
				else if (a.val < b.val)
					return -1;
				else
					return 0;
				 // Assume neither ListNode is null. 
		        // You could also just return x.val - y.val
		        // which would be more efficient.
			}
		};		
		// define a PriorityQueue object
		PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(lists.size(),
				compListNode);
		// travel each ListNode from lists
		Iterator itr = lists.iterator();
		while (itr.hasNext()) {
			ListNode l = (ListNode) itr.next();
			if (l != null) {
				pq.add(l);
			}
			// System.out.println(l.printForward());
		}		
		//merge into a single linkedlist
		ListNode merge = new ListNode(0);
		ListNode cur=merge;
		
		while(pq.size()>0){
			ListNode temp=pq.poll();
			//connect cur->temp;
			cur.next=temp;
			if(temp.next!=null){
				pq.add(temp.next);
			}
			cur=cur.next;
		}		
		return merge.next;
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值