-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path16.cpp
More file actions
70 lines (66 loc) · 1.09 KB
/
16.cpp
File metadata and controls
70 lines (66 loc) · 1.09 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
70
#include <iostream>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) :val(x), next(NULL) {}
ListNode() {}
};
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
ListNode *res = NULL;
if (pHead1 == NULL)
return pHead2;
if (pHead2 == NULL)
return pHead1;
if (pHead1->val < pHead2->val)
{
res = pHead1;
res->next = Merge(pHead1->next, pHead2);
}
else
{
res = pHead2;
res->next = Merge(pHead1, pHead2->next);
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
int n1, n2;
ListNode *L = new ListNode;
ListNode *L1 = new ListNode;
ListNode *L2 = new ListNode;
L->next = NULL;
L1->next = NULL;
L2->next = NULL;
ListNode *q1 = L1;
ListNode *q2 = L2;
cin >> n1 >> n2;
for (int i = 0; i < n1; i++)
{
ListNode *p = new ListNode;
cin >> p->val;
q1->next = p;
q1 = p;
}
q1->next = NULL;
for (int i = 0; i < n2; i++)
{
ListNode *p = new ListNode;
cin >> p->val;
q2->next = p;
q2 = p;
}
q2->next = NULL;
L = Merge(L1->next, L2->next);
while (L != NULL)
{
cout << L->val << " ";
L = L->next;
}
cout << endl;
return 0;
}