-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
163 lines (135 loc) · 4.35 KB
/
Source.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// You are given the heads of two sorted linked lists list1 and list2.
// Merge the two lists into one sorted list.The list should be made by splicing together the nodes of the first two lists.
// Return the head of the merged linked list.
// Assuming the existence of createLinkedList, printList, and deleteLinkedList functions
#include <iostream>
#include <vector>
#include "ListNode.h";
using namespace std;
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2);
ListNode* createLinkedList(const vector<int>& values);
void deleteLinkedList(ListNode* head);
void printList(ListNode* head);
int main() {
// Test Case 1: Both Lists Are Non-Empty and of Equal Length
ListNode* list1 = createLinkedList(vector<int> {1, 3, 5, 7});
ListNode* list2 = createLinkedList(vector<int> {2, 4, 6, 8});
ListNode* mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
// Test Case 2: First List Is Longer Than Second
list1 = createLinkedList(vector<int> {1, 3, 5, 7, 9});
list2 = createLinkedList(vector<int> {2, 4, 6});
mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
// Test Case 3: Second List Is Longer Than First
list1 = createLinkedList(vector<int> {1, 3, 5});
list2 = createLinkedList(vector<int> {2, 4, 6, 8, 10});
mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
// Test Case 4: First List Is Empty
list1 = createLinkedList(vector<int> {});
list2 = createLinkedList(vector<int> {1, 2, 3, 4});
mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
// Test Case 5: Second List Is Empty
list1 = createLinkedList(vector<int> {1, 2, 3, 4});
list2 = createLinkedList(vector<int> {});
mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
// Test Case 6: Both Lists Are Empty
list1 = createLinkedList(vector<int> {});
list2 = createLinkedList(vector<int> {});
mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
// Test Case 7: Lists With Interleaving Values
list1 = createLinkedList(vector<int> {1, 4, 5});
list2 = createLinkedList(vector<int> {2, 3, 6});
mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
// Test Case 8: Lists With Identical Values
list1 = createLinkedList(vector<int> {1, 1, 1});
list2 = createLinkedList(vector<int> {1, 1, 1});
mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
// Test Case 9: Lists With One Element Each
list1 = createLinkedList(vector<int> {1});
list2 = createLinkedList(vector<int> {2});
mergedList = mergeTwoLists(list1, list2);
printList(mergedList);
deleteLinkedList(mergedList);
return 0;
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
// Handle Empty Lists
if (list1 == nullptr && list2 == nullptr) { return nullptr; }
// If one list is empty, return the other
if (list1 == nullptr) { return list2; }
if (list2 == nullptr) { return list1; }
// Create a new list to represent merged list
ListNode* head = nullptr;
if (list1->val <= list2->val) {
head = list1;
list1 = list1->next;
}
else {
head = list2;
list2 = list2->next;
}
ListNode* dummyHead = head;
// Loop through both lists and compare values
while (list1 != nullptr && list2 != nullptr) {
if (list1->val <= list2->val) {
dummyHead->next = list1;
list1 = list1->next;
}
else {
dummyHead->next = list2;
list2 = list2->next;
}
dummyHead = dummyHead->next;
}
// Directly link the remainder of the non-empty list
dummyHead->next = (list1 != nullptr) ? list1 : list2;
return head;
}
ListNode* createLinkedList(const vector<int>& values) {
if (values.empty()) { return nullptr; }
ListNode* head = new ListNode(values[0]);
ListNode* current = head;
for (int i = 1; i < values.size(); ++i) {
current->next = new ListNode(values[i]);
current = current->next;
}
return head;
}
void deleteLinkedList(ListNode* head) {
while (head != nullptr) {
ListNode* temp = head;
head = head->next;
delete temp;
}
}
void printList(ListNode* head) {
int count = 0;
cout << "[";
while (head != nullptr) {
if (count == 0) {
cout << head->val;
head = head->next;
++count;
}
else {
cout << ", " << head->val;
head = head->next;
}
}
cout << "]\n\n";
}