LeetCode --- 23. 合并K个排序链表

解法1:最笨的解法

  • 时间复杂度:O(nlogn)
  • 空间复杂度:O(n)
public class _23_合并K个排序链表 {
    public ListNode mergeKLists1(ListNode[] lists) {
    	if (lists == null || lists.length == 0) return null;
    	
    	// 将所有节点添加到数组中
    	List<ListNode> nodes = new ArrayList<>();
    	for (ListNode list : lists) {
			while (list != null) {
				nodes.add(list);
				list = list.next;
			}
		} // O(n)
    	
    	// 对数组进行排序(基于比较的排序,时间复杂度目前最好是O(nlogn),n是所有节点的数量)
    	nodes.sort((ListNode node1, ListNode node2) -> {
    		return node1.val - node2.val;
    	});
    	// 将排好序的节点串起来
    	ListNode head = new ListNode(0);
    	ListNode cur = head;
    	for (ListNode node : nodes) {
			cur = cur.next = node;
		} // O(n)
    	return head.next;
    }
}

解法2:逐一比较

  • 时间复杂度O(kn)
public class _23_合并K个排序链表 {
	 // 10条链表
	 // 每条链表有1W个节点
	 // k == 10
	 // n == 10W
	 public ListNode mergeKLists2(ListNode[] lists) {
    	if (lists == null || lists.length == 0) return null;
    	ListNode head = new ListNode(0);
    	ListNode cur = head;
    	
    	while (true) {
        	// 最小链表节点所在的索引
        	int minIndex = -1;
        	for (int i = 0; i < lists.length; i++) {
    			if (lists[i] == null) continue;
    			
    			if (minIndex == -1 || lists[i].val < lists[minIndex].val) {
    				minIndex = i;
    			}
    		} // O(k)
        	// 所有链表节点已经串起来了
        	if (minIndex == -1) break;

        	cur = cur.next = lists[minIndex];
        	lists[minIndex] = lists[minIndex].next;
    	} // O(kn)
    	
    	return head.next;
    }
}

解法3:逐一两两合并

  • 时间复杂度:O(kn)
public class _23_合并K个排序链表 {
	public ListNode mergeKLists3(ListNode[] lists) {
    	if (lists == null || lists.length == 0) return null;
    	// k条链表,总共n个节点,每一条链表平均 n/k 个节点
    	for (int i = 1; i < lists.length; i++) { // k - 1次的复杂度累加起来
    		lists[0] = mergeTwoLists(lists[0], lists[i]);
		}
    	return lists[0];
    }
	// 虚拟头结点(dummy head)
    private ListNode head = new ListNode(0);
    public ListNode mergeTwoLists(ListNode k1, ListNode k2) {
    	if (k1 == null) return k2;
    	if (k2 == null) return k1;
    	
    	head.next = null;
    	ListNode cur = head;
    	while (k1 != null && k2 != null) {
    		if (k1.val <= k2.val) {
    			cur = cur.next = k1;
    			k1 = k1.next;
    		} else {
    			cur = cur.next = k2;
    			k2 = k2.next;
    		}
    	}
    	if (k1 == null) {
    		cur.next = k2;
    	} else if (k2 == null) {
    		cur.next = k1;
    	}
    	return head.next;
    }
}

解法4:优先级队列(小顶堆)

  • 时间复杂度:O(nlogk)
  • 空间复杂度:O(k)
public class _23_合并K个排序链表 {
	public ListNode mergeKLists4(ListNode[] lists) {
    	if (lists == null || lists.length == 0) return null;
    	ListNode head = new ListNode(0);
    	ListNode cur = head;
    	// 将所有链表的头结点添加到小顶堆(优先级队列)中
    	PriorityQueue<ListNode> queue = new PriorityQueue<>((ListNode node1, ListNode node2) -> {
    		return node1.val - node2.val;
    	});
    	for (ListNode list : lists) {
    		if (list == null) continue;
			queue.offer(list);
		} // klogk
    	// 不断删除堆顶元素,并且把堆顶元素的next添加到堆中
    	while (!queue.isEmpty()) {
    		// 删除堆顶元素
    		ListNode node = queue.poll();
    		cur = cur.next = node;
    		if (node.next != null) {
    			queue.offer(node.next);
    		}
    	} // nlogk
    	return head.next;
    }
}

解法5:分治策略

  • 时间复杂度:O(nlogk)
public class _23_合并K个排序链表 {
	 public ListNode mergeKLists5(ListNode[] lists) {
    	if (lists == null || lists.length == 0) return null;
    	int step = 1;
    	while (step < lists.length) {
    		int nextStep = step << 1;
        	for (int i = 0; i + step < lists.length; i += nextStep) {
        		lists[i] = mergeTwoLists(lists[i], lists[i + step]);
    		}
        	step = nextStep;
    	}
    	return lists[0];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值