题目
对于K个链表的情况,不先求两个升序链表分治的情况!这正好是leetcode的一个简单题:合并两个有序链表
合并两个有序链表
解法一:新建一个链表,每次插入l1和l2中value小的那一个:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* res = new ListNode;
res->next = NULL;
ListNode* real = res;
int tmp = 0;
while(l1!=NULL&&l2!=NULL){
if(l1->val <= l2->val){
tmp = l1->val;
l1 = l1->next;
}
else{
tmp = l2->val;
l2 = l2->next;
}
ListNode* p = new ListNode;
p->val = tmp;
p->next = NULL;
res->next = p;
res = res->next;
}
if(l1 == NULL){
res->next = l2;
}
else
res->next = l1;
return real->next;
}
};
解法二:不用新建链表,直接在原链表上操作,修改原链表的指向:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
ListNode head,*tail;
head.next = NULL;
tail = &head;
ListNode* tmp;
while(l1!=NULL&&l2!=NULL){
if(l1->val<l2->val){
//tmp = l1->val;
tmp = l1;
l1 = l1->next;
}
else{
//tmp = l2->val;
tmp = l2;
l2 = l2->next;
}
// ListNode* p = new ListNode;
// p->val = tmp;
// p->next = NULL;
tail->next = tmp;
tail = tail->next;
}
if(l1!=NULL)
tail->next = l1;
else
tail->next = l2;
return head.next;
}
};
解法三:比较高端的解法,可以使用递归:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == nullptr) {
return l2;
} else if (l2 == nullptr) {
return l1;
} else if (l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
K个链表的情况
解法一:比较容易想到的是使用我们前面得出的两个升序链表合并的解法,进行链表挨个挨个合并:
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0)
return NULL;
ListNode* res = lists[0];
if(lists.size() == 1)
return res;
for(int i=1;i<lists.size();i++){
res = DFS(res, lists[i]);
}
return res;
}
ListNode* DFS(ListNode* p1, ListNode* p2){
if(p1 == NULL)
return p2;
if(p2 == NULL)
return p1;
if(p1->val<=p2->val){
p1->next = DFS(p1->next, p2);
return p1;
}
else{
p2->next = DFS(p2->next, p1);
return p2;
}
}
};
解法二:采用分治法,不是逐个链表的合并,而是两两合并:
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0)
return NULL;
ListNode* res = lists[0];
if(lists.size() == 1)
return res;
return merge(lists,0,lists.size()-1);
}
ListNode* merge(vector<ListNode*>& lists, int left, int right){
if(left == right)
return lists[left];
if(left>right)
return NULL;
int mid = (left+right)/2;
return DFS(merge(lists,left,mid),merge(lists,mid+1,right));
}
ListNode* DFS(ListNode* p1, ListNode* p2){
if(p1 == NULL)
return p2;
if(p2 == NULL)
return p1;
if(p1->val<=p2->val){
p1->next = DFS(p1->next, p2);
return p1;
}
else{
p2->next = DFS(p2->next, p1);
return p2;
}
}
};
解法三:优先队列,使用C++ STL的priority_queue来动态地对每个链表进行排序:
class Solution {
public:
struct LN{
ListNode* p;
bool operator<(const LN& a) const
{
return p->val>a.p->val;
}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0)
return NULL;
priority_queue<LN> queue;
for(int i=0;i<lists.size();i++){
if(lists[i]!=NULL){
queue.push({lists[i]});
}
}
ListNode head;
ListNode* tail = &head;
while(!queue.empty()){
tail->next = queue.top().p;
tail = tail->next;
queue.pop();
if(tail->next == NULL)
continue;
queue.push({tail->next});
}
return head.next;
}
};