【leetcode 链表】 Merge Two Sorted Lists 和 Merge k Sorted Lists
2014-11-07 23:42
417 查看
Merge Two Sorted
1、题目
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodesof the first two lists
将两个已排序的链表L1和L2 merge sort
2、分析
#1 新建一个结点dummy,初始时tail指向dummy;head1、head2分别指向L1、L2的头结点#2 每次循环都将tail指向head1、head2中val较小者,更新tail、head1/head2
#3 跳出循环后的处理
3、代码
<span style="font-size:18px;">/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode dummy(-1); ListNode *head1=l1,*head2=l2,*tail=&dummy; while(head1!=nullptr && head2!=nullptr) { if(head1->val<head2->val) {tail->next=head1;head1=head1->next;} else {tail->next=head2;head2=head2->next;} tail=tail->next; } if(head1==nullptr) tail->next=head2; if(head2==nullptr) tail->next=head1; /*两句可以合并为 tail->next=head1==nullptr?head2:head1; */ return dummy.next; } };</span>
Merge k Sorted Lists
1、题目
Merge k sorted linked lists and return it as one sorted list.Analyze and describe its complexity.
2、分析
利用上题的函数mergeTwoLists()两种方法
1、一种是逐步地merge两个链表,形成新的链表,再将新的链表跟另一个链表merge,时间复杂度是O(n(n1+n2+n3....)) n是链表个数
【
因为merge L1和L2花费O(L1+L2),形成新的链表的长度为n1+n2,merge该新链表与L3花费O((n1+n2)+n3).....所以总的是
O((n1+n2)+((n1+n2)+n3)+.......)
】
2、另外一种是用归并排序的思想,时间复杂度是O(logn(n1+n2+n3+......)),因为是两两merge,递归树有logn层,每一层的代价
都为O(n1+n2+n3+......)。假设有4个链表,下面为递归树:
3、代码
<span style="font-size:18px;">class Solution { public: ListNode *mergeKLists(vector<ListNode *> &lists) { /*归并排序*/ int n=lists.size(); if(n==0) return nullptr; if(n==1) return lists[0]; /*vector<ListNode *> list_left(lists.begin(),lists.begin()+n/2-1); vector<ListNode *> list_right(lists.begin()+n/2,lists.end()); 这两句有问题,没有把所有元素都装进list_left、list_right,不知道为什么?下面用for循环来装 */ vector<ListNode *> list_left; vector<ListNode *> list_right; auto iter=lists.begin(); for(;iter<lists.begin()+n/2;iter++) list_left.push_back(*iter); for(;iter!=lists.end();iter++) list_right.push_back(*iter); ListNode *l1=mergeKLists(list_left); ListNode *l2=mergeKLists(list_right); return mergeTwoLists(l1,l2); } private: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode dummy(-1); ListNode *head1=l1,*head2=l2,*tail=&dummy; while(head1!=nullptr && head2!=nullptr) { if(head1->val<head2->val) {tail->next=head1;head1=head1->next;} else {tail->next=head2;head2=head2->next;} tail=tail->next; } if(head1==nullptr) tail->next=head2; if(head2==nullptr) tail->next=head1; return dummy.next; } };</span>
相关文章推荐
- LeetCode-21- Merge Two Sorted Lists(合并两个已排序链表)
- [LeetCode][链表]Merge Two Sorted Lists
- leetcode Merge Two Sorted Lists 合并两个有序链表
- leetCode 21. Merge Two Sorted Lists 合并链表
- [C++]LeetCode 21: Merge Two Sorted Lists(合并链表)
- LeetCode 21. Merge Two Sorted Lists(合并链表)
- LeetCode Merge Two Sorted Lists(合并两个有序链表)
- LeetCode(Merge Two Sorted Lists ) 合并两个有序的链表
- LeetCodet题解--21. Merge Two Sorted Lists(合并两个排序好的链表)
- LeetCode | Merge Two Sorted Lists(合并两个链表)
- [LeetCode21]Merge Two Sorted Lists(合并两个有序链表)
- Leetcode刷题记——21. Merge Two Sorted Lists(合并两有序链表)
- LeetCode 21 Merge Two Sorted Lists (有序两个链表整合)
- leetcode:Merge Two Sorted Lists(有序链表的归并)
- LeetCode-21-Merge Two Sorted Lists(链表/归并)-Easy
- 面试题17:合并两个有序链表(Leetcode-21:Merge Two Sorted Lists)
- LeetCode 21 Merge Two Sorted Lists 把两个链表有序连接
- LeetCode 21. Merge Two Sorted Lists(链表)
- leetcode:Merge Two Sorted Lists (链接两个有序链表)【面试算法题】
- leetcode 题解Merge Two Sorted Lists(有序链表归并)