Skip to content

Commit 4c1400d

Browse files
committed
fix: fixed test case failing
1 parent 49c2e2a commit 4c1400d

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

search/median_search2.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ ListNode* middleNode(ListNode* head) {
7070
slowptr = slowptr->next;
7171
fastptr = fastptr->next->next;
7272
}
73+
7374
return (fastptr->next) ? slowptr->next : slowptr;
7475
}
7576
} // namespace median_search2
@@ -80,51 +81,57 @@ ListNode* middleNode(ListNode* head) {
8081
* @returns void
8182
*/
8283
static void test() {
83-
auto* head = new ListNode;
84-
head->val = 1;
84+
auto* head1 = new ListNode;
85+
head1->val = 1;
8586

86-
ListNode* temp1 = head;
87+
ListNode* temp = head1;
8788
for (int i = 1; i < 6; ++i) {
88-
auto temp2 = new ListNode;
89-
temp2->val = i;
89+
auto* temp1 = new ListNode;
90+
temp1->val = i;
9091

91-
temp1->next = temp2;
92-
temp1 = temp2;
92+
temp->next = temp1;
93+
temp = temp1;
9394
}
9495

95-
ListNode* median = search::median_search2::middleNode(head);
96+
ListNode* median = search::median_search2::middleNode(head1);
9697
assert(3 == median->val); // 3 is the value of the median node.
9798
std::cout << "test case:1 passed\n";
98-
99+
99100
// Clean up
100-
while (head) {
101-
ListNode* t = head;
102-
head = head->next;
101+
while (head1) {
102+
ListNode* t = head1;
103+
head1 = head1->next;
103104
delete t;
104105
}
105-
106-
head = new ListNode;
107-
head->val = 1;
108-
109-
temp1 = head;
106+
delete head1;
107+
delete temp;
108+
109+
// Test case # 2
110+
auto* head2 = new ListNode;
111+
head2->val = 1;
112+
113+
ListNode* temp2 = head2;
110114
for (int i = 1; i < 7; ++i) {
111-
auto temp2 = new ListNode;
112-
temp2->val = i;
115+
auto temp3 = new ListNode;
116+
temp3->val = i;
113117

114-
temp1->next = temp2;
115-
temp1 = temp2;
118+
temp2->next = temp3;
119+
temp2 = temp3;
116120
}
117121

118-
median = search::median_search2::middleNode(head);
119-
assert(4 == median->val); // 3 is the value of the median node.
122+
median = search::median_search2::middleNode(head2);
123+
assert(4 == median->val); // 4 is the value of the median node.
120124
std::cout << "test case:1 passed\n";
121125

122126
// Clean up
123-
while (head) {
124-
ListNode* t = head;
125-
head = head->next;
127+
while (head2) {
128+
ListNode* t = head2;
129+
head2 = head2->next;
126130
delete t;
127131
}
132+
delete head2;
133+
delete temp2;
134+
128135
std::cout << "test case:2 passed\n";
129136
std::cout << "--All tests passed--\n";
130137
}

0 commit comments

Comments
 (0)