-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergekSortedLists.cpp
More file actions
69 lines (59 loc) · 1.19 KB
/
MergekSortedLists.cpp
File metadata and controls
69 lines (59 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<vector>
using namespace std;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode *head;
head=NULL;
for(int i=0;i<lists.size();i++){
head=mergeTwoLists(head,lists.at(i));
}
return head;
}
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==NULL){
return l2;
}
if(l2==NULL){
return l1;
}
ListNode *p,*q,*head;
if(l1->val>l2->val){
head=l2;
q=l1;
}else{
head=l1;
q=l2;
}
p=head;
ListNode *last,*next;
while(p->next!=NULL&&q!=NULL){
cout<<"p: "<<p->val<<endl;
if(p->val<=q->val&&p->next->val>=q->val){
cout<<"p: "<<p->val<<"------"<<"q: "<<q->val<<endl;
last=q;
while(q->next!=NULL&&p->val<=q->next->val&&p->next->val>=q->next->val){
q=q->next;
cout<<"q: "<<q->val<<endl;
}
cout<<"q: "<<q->val<<endl;
next=q;
q=q->next;
next->next=p->next;
p->next=last;
p=next;
}
p=p->next;
}
if(p->next==NULL){
p->next=q;
}
return head;
}
};