(Java)LeetCode-23. Merge k Sorted Lists

原创 2016年06月02日 00:23:11

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


首先说明这道题我的代码运行时间可以排进前70%呀~开心~还是hard模式

这道题首先想到的是用k个指针分别指向每个List的头部,再其次遍历,不过这样每次都要求k个数里面的最小值,不好不好

然后思路是lists[0]和lists[1]先合并,再合并lists[2],依次到最后一个,这样时间复杂度是KM(M是总的节点个数,K是列表数)

最后我看到的Tags里有分治,于是就想到了归并排序,先排序左半部分,再排序后半部分,最后两个List合并,排左半部分的时候同样如此左右拆分,直到不能拆为止,和归并排序的想法一致。  采用归并排序的时间复杂度是MlogK,每层比较M个点,总共logK 层。代码如下~


/**
 * 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.length == 0){
	        return null;
	    }
		ListNode res = merge(lists,0,lists.length-1);
		return res;	    
	}

	private ListNode merge(ListNode[] lists, int left, int right) {
		// TODO Auto-generated method stub
		if(right <= left){
			return lists[right];
		}
		
		int mid = left + (right - left)/2;
		ListNode Listleft = merge(lists,left,mid);
		ListNode Listright= merge(lists,mid+1,right);
		
		ListNode res = mergetwo(Listleft,Listright);
		
		
		return res;
	}

	private ListNode mergetwo(ListNode listleft, ListNode listright) {
		// TODO Auto-generated method stub
		ListNode head = new ListNode(-1);
		ListNode temp = head;
		while(listleft != null || listright != null){
			if(listleft == null){
				head.next = listright;
				return temp.next;
			}else if(listright == null){
				head.next = listleft;
				return temp.next;
			}else if(listleft.val >= listright.val){
				head.next = listright;
				head = head.next;
				listright = listright.next;
			}else{
				head.next = listleft;
				head = head.next;
				listleft = listleft.next;
			}
		}		
		return temp.next;
	}	
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode] 23. Merge k Sorted Lists java

/**23. Merge k Sorted Lists * @param lists * @return 合并k路有序链表 */ public ListNode ...

LeetCode 23 Merge k Sorted Lists (C,C++,Java,Python)

Problem: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its c...

【LeetCode】23. Merge k Sorted Lists 基于Java的解法

23. Merge k Sorted Lists Total Accepted: 88102 Total Submissions: 372066 Difficulty: Hard Merge k...

leetcode解题之23.Merge k Sorted Lists Java版本(合并k个有序的链表)

leetcode解题之23.Merge k Sorted Lists Java版本(合并k个有序的链表)

leetcode23~Merge k Sorted Lists Add to List

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

【leetcode】23. Merge k Sorted Lists

合并多个已排序的单链表,多种实现方式

[leetcode] 23. Merge k Sorted Lists

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

leetcode_23_Merge k Sorted Lists

思路: 本来想模仿合并两个链表的方法,查找所有的lists链表一遍找出一个最小值,并将最小值链接到结果链表中,并将最小结点删除,将最小结点的next结点替代之,直至为null,将链表删去。 但是当l...

LeetCode_OJ【23】Merge k Sorted Lists

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

leetcode--23. Merge k Sorted Lists

leetcode
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)