- 第一题:MergedKSortedList
- 题意:给定了K个有序单向的list的第一个节点,让给返回将这K个List合并后的List的首节点
- 思路1:每次比较K个list的当前节点,选出最小的,加入result的list中。直到K个list都遍历完成。
- 思路2:2分法合并,首先利用二分法将其分成两两list,然后再合并两两list,直到最后合并完成。
- 思路3:利用最优队列,将所有点加入到最优队列中去,排序的依据就是节点的val从小到大排序。然后再一次将所有点弹出即可。
- 三种方法的代码代码如下,这三种方法的耗时逐渐减少,具体耗时见下图:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
ListNode* ListNodeFromVector(vector<int> xs){
ListNode* head = this;
for(int i=0;i<xs.size();i++){
head->next = new ListNode(xs[i]);
head = head->next;
}
return this->next;
}
void print(){
ListNode* head = this;
while(head != NULL){
cout<<head->val<<"->";
head = head->next;
}
cout<<endl;
}
};
struct cmp{
bool operator()(ListNode* a, ListNode* b){
return a->val>b->val;
}
};
class Solution {
public:
ListNode* mergeBinaryList(ListNode* l1, ListNode* l2){
ListNode* res = new ListNode(0);
ListNode* head = res;
while(l1 != NULL && l2 != NULL){
if(l1->val < l2->val){
res->next = new ListNode(l1->val);
l1 = l1->next;
res = res->next;
}else{
res->next = new ListNode(l2->val);
l2 = l2->next;
res = res->next;
}
}
if(l1 != NULL){
res->next = l1;
}
if(l2 != NULL){
res->next = l2;
}
return head->next;
}
ListNode* binary_divided(vector<ListNode*>& lists, int s, int e){
if(s == e){
return lists[s];
}else{
if(s < e){
int mid = (s + e) / 2;
ListNode* l1 = binary_divided(lists, s, mid);
ListNode* l2 = binary_divided(lists, mid + 1, e);
return mergeBinaryList(l1, l2);
}else{
return NULL;
}
}
}
void generate(vector<ListNode*>& lists, ListNode* head){
ListNode** min_node = NULL;
for(int i=0;i<lists.size();i++){
if(lists[i] != NULL){
if(min_node == NULL){
min_node = &lists[i];
}else{
if((*min_node)->val > lists[i]->val){
min_node = &lists[i];
}
}
}else{
continue;
}
}
if(min_node == NULL){
return ;
}
head->next = new ListNode((*min_node)->val);
*min_node = (*min_node)->next;
generate(lists, head->next);
}
ListNode* priority_queue_method(vector<ListNode* > lists){
priority_queue<int, vector<int >, greater<int> > queue1;
for(int i=0;i<lists.size();i++){
ListNode* head = lists[i];
while(head != NULL){
queue1.push(head->val);
head = head->next;
}
}
ListNode* res = new ListNode(0);
ListNode* head = res;
while(not queue1.empty()){
res->next = new ListNode(queue1.top());
queue1.pop();
res = res->next;
}
return head->next;
}
ListNode* priority_queue_method_v2(vector<ListNode* > lists){
priority_queue<ListNode*, vector<ListNode* >, cmp> queue1;
for(int i=0;i<lists.size();i++){
if(lists[i] != NULL)
queue1.push(lists[i]);
}
ListNode* res = new ListNode(0);
ListNode* head = res;
while(not queue1.empty()){
ListNode* top = queue1.top();
res->next = new ListNode(top->val);
queue1.pop();
res = res->next;
if(top->next != NULL){
queue1.push(top->next);
}
}
return head->next;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
return priority_queue_method_v2(lists);
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d25642ac0099aa7a677b14b2070abd4c.png)
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
ListNode* ListNodeFromVector(vector<int> xs){
ListNode* head = this;
for(int i=0;i<xs.size();i++){
head->next = new ListNode(xs[i]);
head = head->next;
}
return this->next;
}
void print(){
ListNode* head = this;
while(head != NULL){
cout<<head->val<<"->";
head = head->next;
}
cout<<endl;
}
};
class Solution {
public:
ListNode* reverseKGroup_v2(ListNode* head, int k){
ListNode *res = new ListNode(0);
ListNode *front_head = res;
res->next = head;
ListNode* l = head;
ListNode* r = head;
while(true){
int count = 0;
while(count<k && r != NULL){
r = r->next;
count += 1;
}
if(count == k){
ListNode* pre = r;
ListNode* cur = l;
for(int i=0;i<count;i++){
ListNode* second = cur->next;
cur->next = pre;
ListNode* original_cur = cur;
cur = second;
pre = original_cur;
}
front_head->next = pre;
front_head = l;
l = r;
}else{
return res->next;
}
}
}
ListNode* reverseKGroup(ListNode* head, int k) {
queue<int > xs;
while(head != NULL){
xs.push(head->val);
head = head->next;
}
ListNode* tmp = new ListNode(0);
ListNode* res = tmp;
while(xs.size() >= k){
stack<int> xs_stack;
for(int i=0;i<k;i++){
xs_stack.push(xs.front());
xs.pop();
}
while(not xs_stack.empty()){
tmp->next = new ListNode(xs_stack.top());
xs_stack.pop();
tmp = tmp->next;
}
}
while(not xs.empty()){
tmp->next = new ListNode(xs.front());
xs.pop();
tmp = tmp->next;
}
return res->next;
}
};