解题思路:(C#)
用分治法把大问题划成小块,同时使用递归求解思想解决问题。新建两个函数,一个用来递归拆分链表,直至拆分成两个之间的比较;另一个利用合并两个有序链表的思想将两个合并,最后将所有拆分的链表回归为一个完整的链表完成递归。
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode MergeKLists(ListNode[] lists) {
if(lists==null||lists.Length==0)
return null;
return Merge(lists,0,lists.Length-1);
}
public ListNode Merge(ListNode[] lists,int start,int end)
{
if (start>=end)
{
return lists[start];
}
int mid = start+(end-start)/2;
ListNode l1 = Merge(lists,start,mid);
ListNode l2 = Merge(lists,mid+1,end);
return mergeTwoLists(l1,l2);
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyNode = new ListNode(-1);
ListNode pre = dummyNode;
if(l1==null)
{
return l2;
}
if(l2==null)
{
return l1;
}
while(l1!=null&&l2!=null)
{
if(l1.val>=l2.val)
{
pre.next = l2;
l2 = l2.next;
}
else
{
pre.next= l1;
l1 = l1.next;
}
pre = pre.next;
}
if(l1!=null)
{
pre.next = l1;
}
if(l2!=null)
{
pre.next = l2;
}
return dummyNode.next;
}
}