-
Notifications
You must be signed in to change notification settings - Fork 0
/
test52.cpp
84 lines (77 loc) · 1.57 KB
/
test52.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct ListNode {
int val;
ListNode* next;
};
void addListNode(ListNode* head, int n)
{
ListNode* p = head;
ListNode* newNode = new ListNode;
while (p->next)
{
p = p->next;
}
newNode->val = n;
newNode->next = nullptr;
p->next = newNode;
}
void printNode(ListNode* head)
{
ListNode* p = head->next;
while (p)
{
std::cout << p->val << ' ';
p = p->next;
}
std::cout << endl;
}
ListNode* createList(int mode, int num = 10, int x = 1)
{
ListNode* list = new ListNode{ -1, nullptr };
if (mode == 1) {
for (int i = 1; i < num; i++)
{
addListNode(list, rand() % 100 * x);
}
}
else
{
for (int i = 1; i < num + 1; i++)
{
addListNode(list, i * x);
}
}
return list;
}
ListNode* merge_sorted_lists(ListNode* ha, ListNode* hb) {
ListNode* dummy = new ListNode{ 0, nullptr };
ListNode* curr = dummy;
while (ha && hb) {
if (ha->val < hb->val) {
curr->next = ha;
ha = ha->next;
}
else {
curr->next = hb;
hb = hb->next;
}
curr = curr->next;
}
if (ha) curr->next = ha;
if (hb) curr->next = hb;
return dummy->next;
}
int main()
{
ListNode* l1 = createList(2,10,1),
* l2 = createList(2,10,2),
* l3{};
printNode(l1);
printNode(l2);
l3 = merge_sorted_lists(l1->next, l2->next);
printNode(l3);
}