C++
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->val<=list2->val){ list1->next = mergeTwoLists(list1->next,list2); return list1; } else{ list2->next = mergeTwoLists(list1,list2->next); return list2; } } };
|
Python
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
|
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 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:
示例 3:
C++
顺序合并
来源:力扣(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->val<=list2->val){ list1->next = mergeTwoLists(list1->next,list2); return list1; } else{ list2->next = mergeTwoLists(list1,list2->next); return list2; } } };
|
分治合并
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->val<=list2->val){ list1->next = mergeTwoLists(list1->next,list2); return list1; } else{ list2->next = mergeTwoLists(list1,list2->next); return list2; } } };
|
Python
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
|