-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17_MergeSortedLists2.cpp
108 lines (96 loc) · 2.24 KB
/
17_MergeSortedLists2.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using namespace std;
struct ListNode
{
int m_nValue;
ListNode* m_pNext;
};
ListNode* Merge(ListNode* head1, ListNode* head2)
{
if (head1 == NULL && head2 == NULL)
return NULL;
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
if (head1->m_nValue < head2->m_nValue)
{
head1->m_pNext = Merge(head1->m_pNext, head2);
return head1;
}
else
{
head2->m_pNext = Merge(head1, head2->m_pNext);
return head2;
}
}
void PrintList(ListNode* head)
{
if (head != NULL)
{
ListNode* p = head;
while (p != NULL)
{
cout << p->m_nValue << endl;
p = p->m_pNext;
}
cout << endl;
}
}
void CreateList(int* nums, int length, ListNode** pHead)
{
*pHead = NULL;
if (nums == NULL || length <= 0)
return;
for (int i = length-1; i >= 0; i--)
{
ListNode* newNode = new ListNode;
newNode->m_nValue = nums[i];
newNode->m_pNext = *pHead;
*pHead = newNode;
}
}
int main()
{
int nums1[] = {1, 3, 5, 7};
int nums2[] = {2, 4, 6, 8};
ListNode* head1;
ListNode* head2;
CreateList(nums1, 4, &head1);
CreateList(nums2, 4, &head2);
ListNode* head12 = Merge(head1, head2);
PrintList(head12);
int nums3[] = {2};
int nums4[] = {1, 3};
ListNode* head3;
ListNode* head4;
CreateList(nums3, 1, &head3);
CreateList(nums4, 2, &head4);
ListNode* head34 = Merge(head3, head4);
PrintList(head34);
int nums5[] = {3};
int nums6[] = {5};
ListNode* head5;
ListNode* head6;
CreateList(nums5, 1, &head5);
CreateList(nums6, 1, &head6);
ListNode* head56 = Merge(head5, head6);
PrintList(head56);
int nums7[] = {3, 4};
int* nums8 = NULL;
ListNode* head7;
ListNode* head8;
CreateList(nums7, 2, &head7);
CreateList(nums8, 0, &head8);
ListNode* head78 = Merge(head7, head8);
PrintList(head78);
int* nums9 = NULL;
int* nums10 = NULL;
ListNode* head9;
ListNode* head10;
CreateList(nums9, 0, &head9);
CreateList(nums10, 0, &head10);
ListNode* head90 = Merge(head9, head10);
PrintList(head90);
return 0;
}