0%

链表之合并有序链表合集

21. 合并两个有序链表

C++

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if(list1 == nullptr && list2 == nullptr) return nullptr;

if(list1 && list2 == nullptr) return list1;
if(list2 && list1 == nullptr) return list2;

//上面三行等价于 if(!list1 || !list2) return l1?l1:l2;

if(list1->val<=list2->val){
list1->next = mergeTwoLists(list1->next,list2);
return list1;
}
else{
list2->next = mergeTwoLists(list1,list2->next);
return list2;
}
}
};

Python

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1, list2):
if not list1 or not list2: return list1 if list1 else list2
if list1.val<=list2.val:
list1.next = self.mergeTwoLists(list1.next,list2)
return list1
else:
list2.next = self.mergeTwoLists(list1,list2.next)
return list2

23. 合并K个升序链表

给你一个链表数组,每个链表都已经按升序排列。

请你将所有链表合并到一个升序链表中,返回合并后的链表。

示例 1:

输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6

示例 2:

输入:lists = []
输出:[]

示例 3:

输入:lists = [[]]
输出:[]

C++

顺序合并
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/

//用一个变量 ans 来维护以及合并的链表,第 i次循环把第 i 个链表和 ans 合并,答案保存到 ans 中。

来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
ListNode* ans=nullptr;
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size()==0) return nullptr;
if(lists.size()==1) return lists[0];
if(lists.size()==2) return mergeTwoLists(lists[0],lists[1]);


ans = mergeTwoLists(lists[0],lists[1]);
for(int i =2;i<lists.size();i++){
ans = mergeTwoLists(ans,lists[i]);
}
return ans;

}

ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if(list1 == nullptr && list2 == nullptr) return nullptr;

if(list1 && list2 == nullptr) return list1;
if(list2 && list1 == nullptr) return list2;

//上面三行等价于 if(!list1 || !list2) return l1?l1:l2;

if(list1->val<=list2->val){
list1->next = mergeTwoLists(list1->next,list2);
return list1;
}
else{
list2->next = mergeTwoLists(list1,list2->next);
return list2;
}
}
};
分治合并
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* merge(vector<ListNode*> lists,int l,int r){
if(l==r) return lists[l];
if(l>r) return nullptr;
int mid =(l+r)/2;
return mergeTwoLists(merge(lists,l,mid),merge(lists,mid+1,r));
}

ListNode* mergeKLists(vector<ListNode*>& lists) {
return merge(lists,0,lists.size()-1);
}

ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if(list1 == nullptr && list2 == nullptr) return nullptr;

if(list1 && list2 == nullptr) return list1;
if(list2 && list1 == nullptr) return list2;

//上面三行等价于 if(!list1 || !list2) return l1?l1:l2;

if(list1->val<=list2->val){
list1->next = mergeTwoLists(list1->next,list2);
return list1;
}
else{
list2->next = mergeTwoLists(list1,list2->next);
return list2;
}
}
};

Python

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
n = len(lists)
return self.merge(lists, 0, n-1)

def merge(self,lists, left, right):
if left>right: return None
if left == right: return lists[left]
mid = left + (right - left) // 2
l1 = self.merge(lists, left, mid)
l2 = self.merge(lists, mid+1, right)
return self.mergeTwoLists(l1, l2)

def mergeTwoLists(self, list1, list2):
if not list1 or not list2: return list1 if list1 else list2
if list1.val<=list2.val:
list1.next = self.mergeTwoLists(list1.next,list2)
return list1
else:
list2.next = self.mergeTwoLists(list1,list2.next)
return list2