leetcode 21|23. Merge Two|k Sorted Lists
来源:互联网 发布:nerf淘宝 编辑:程序博客网 时间:2024/06/06 06:31
21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* mergeTwoLists(ListNode* p1, ListNode* p2) { //way-1 /* if(p1==NULL) return p2; if(p2==NULL) return p1; ListNode *result,*middle,*q,*nex; if(p1->val<p2->val) { result=p1; q=p1; middle=p2; } else { result=p2; q=p2; middle=p1; } while(q->next) { if(q->next->val<=middle->val) { q=q->next; } else { nex=q->next; q->next=middle; q=middle; middle=nex; } } q->next=middle; return result; */ //way-2 if(p1==NULL) return p2; if(p2==NULL) return p1; multimap<int,ListNode*> mm; mm.insert(pair<int,ListNode*>(p1->val,p1)); mm.insert(pair<int,ListNode*>(p2->val,p2)); ListNode head(-1); ListNode *p=&head; while(!mm.empty()) { auto it=mm.begin(); p->next=it->second; p=p->next; ListNode *q=it->second->next; if(q!=NULL) mm.insert(pair<int,ListNode*>(q->val,q)); mm.erase(it); } return head.next; }};
23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
way-1:分治算法
每次从数组中取两个链表,将合并结果加入到链表中,反复这个过程,直到数组中只剩一个链表为止,对两个链表进行合并的代码可以复用LeetCode21的代码。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:ListNode* merge2Lists(ListNode* a,ListNode* b){ ListNode head(-1); head.next=(a->val > b->val)?b:a; ListNode * LastPoint=head.next; ListNode * WillIn=(a->val > b->val)?a:b; while(1) { if(!LastPoint->next) { LastPoint->next=WillIn; return head.next; } else if(!WillIn) return head.next; else { if(LastPoint->next->val > WillIn->val) { ListNode * temp=LastPoint->next; LastPoint->next=WillIn; WillIn=temp; LastPoint=LastPoint->next; } else { LastPoint=LastPoint->next; } } } return head.next;}ListNode* mergeKLists(vector<ListNode*>& lists){if (lists.size() == 0)return NULL;while(lists.size()>1){ if(lists[0]==NULL)//erase null list { lists.erase(lists.begin()); continue; } else if(lists[1]==NULL) { lists.erase(lists.begin()+1); continue; } else { lists[0]=merge2Lists(lists[0],lists[1]); lists.erase(lists.begin()+1); }}return lists[0];}};
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* mergeKLists(vector<ListNode*>& lists) { multimap<int, ListNode*> mm; for(int i=0;i<lists.size();i++) { if(lists[i]==NULL) { lists.erase(lists.begin()+i); i--; continue; } else mm.insert(pair<int,ListNode*>(lists[i]->val, lists[i])); } ListNode head(-1); ListNode *p=&head; while(!mm.empty()) { auto it = mm.begin(); p->next = it->second; p = p->next; ListNode *q = it->second->next; if(q != NULL) mm.insert(pair<int,ListNode*>(q->val, q)); mm.erase(it); } return head.next; }};
阅读全文
0 0
- leetcode 21|23. Merge Two|k Sorted Lists
- 【LeetCode】Merge Two Sorted Lists & Merge k Sorted Lists
- leetcode 23. LeetCode Merge k Sorted Lists
- LeetCode(21)Merge Two Sorted Lists
- [LeetCode 21] Merge Two Sorted Lists Solution
- [leetcode 21] Merge Two Sorted Lists
- LeetCode 21:Merge Two Sorted Lists
- LeetCode | #21 Merge Two Sorted Lists
- [Leetcode] 21 - Merge Two Sorted Lists
- leetcode.21------------Merge Two Sorted Lists
- [leetcode]21 Merge Two Sorted Lists
- Leetcode NO.21 Merge Two Sorted Lists
- leetcode[21 链表]Merge Two Sorted Lists
- leetcode-21 Merge Two Sorted Lists
- leetCode #21 Merge Two Sorted Lists
- leetcode 21 Merge Two Sorted Lists
- [Leetcode 21, Easy] Merge Two Sorted Lists
- LeetCode 21 Merge Two Sorted Lists
- 面试宝典1
- linux系统基础命令和知识总结
- 【iOS报错】“Internal error. Please file a bug at bugreport.apple.com and attach "/var/folders/v5/......”
- HttpClient+AsyncTask
- 模拟spring,自定义容器一(属性注入、Bean的实例化)
- leetcode 21|23. Merge Two|k Sorted Lists
- python面向对象编程(3)
- 目标检测评价指标
- leetcode Missing Number 消失的数
- Yaw Pitch Roll的变换顺序问题
- jquery的ajax提交form表单
- HTTP与HTTPS的区别(2)
- jQuery操作input值总结
- imageloader的配置