Skip to content

Commit f859d87

Browse files
author
kwu130
committed
feat(src, common): add 19-removeNthFromEnd and reconstruct listHelper
1 parent 345628b commit f859d87

3 files changed

Lines changed: 118 additions & 43 deletions

File tree

‎include/common.h‎

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,41 @@ struct ListNode {
1616
ListNode(int x, ListNode* next)
1717
: val(x),
1818
next(next) {}
19-
ListNode(const std::vector<int>& vec)
20-
: next(nullptr) {
21-
assert(!vec.empty());
22-
val = vec[0];
23-
ListNode* cur = this;
24-
for (int i = 1; i < vec.size(); i++) {
25-
cur->next = new ListNode(vec[i]);
26-
cur = cur->next;
27-
}
19+
};
20+
21+
ListNode* build(const std::vector<int>& vec) {
22+
if (vec.empty()) return nullptr;
23+
ListNode* head = new ListNode(vec[0]);
24+
ListNode* cur = head;
25+
for (int i = 1; i < vec.size(); i++) {
26+
cur->next = new ListNode(vec[i]);
27+
cur = cur->next;
2828
}
2929

30-
bool equal(const ListNode* other) const {
31-
const ListNode* current_this = this;
32-
const ListNode* current_other = other;
30+
return head;
31+
}
3332

34-
while (current_this != nullptr && current_other != nullptr) {
35-
if (current_this->val != current_other->val) {
36-
return false;
37-
}
38-
current_this = current_this->next;
39-
current_other = current_other->next;
33+
bool equal(const ListNode* lhs, const ListNode* rhs) {
34+
while (lhs != nullptr && rhs != nullptr) {
35+
if (lhs->val != rhs->val) {
36+
return false;
4037
}
38+
lhs = lhs->next;
39+
rhs = rhs->next;
40+
}
41+
42+
return lhs == nullptr && rhs == nullptr;
43+
}
4144

42-
return current_this == nullptr && current_other == nullptr;
45+
void print(const ListNode* head) {
46+
while (head != nullptr) {
47+
std::cout << head->val << " -> ";
48+
head = head->next;
4349
}
44-
};
50+
std::cout << "nullptr" << std::endl;
51+
}
4552

46-
void freeList(ListNode* head) {
53+
void free(ListNode* head) {
4754
while (head != nullptr) {
4855
ListNode* tmp = head;
4956
head = head->next;
@@ -52,6 +59,7 @@ void freeList(ListNode* head) {
5259
}
5360
} // namespace listHelper
5461

62+
5563
namespace compareHelper {
5664
template <typename T>
5765
bool equalIgnoreOrder(std::vector<std::vector<T>> a, std::vector<std::vector<T>> b) {

‎src/19-removeNthFromEnd.cpp‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Created by 1412 on 2025/10/15.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
#include "../include/common.h"
7+
8+
using namespace std;
9+
using listHelper::ListNode;
10+
11+
class Solution {
12+
public:
13+
ListNode* removeNthFromEnd(ListNode* head, int n) {
14+
ListNode *pre, *first, *second;
15+
pre = nullptr;
16+
first = second = head;
17+
while (n--) {
18+
first = first->next;
19+
}
20+
while (first != nullptr) {
21+
first = first->next;
22+
pre = second;
23+
second = second->next;
24+
}
25+
if (pre == nullptr) {
26+
head = head->next;
27+
} else {
28+
pre->next = second->next;
29+
}
30+
delete second;
31+
32+
return head;
33+
}
34+
};
35+
36+
void test() {
37+
Solution s;
38+
ListNode* head = listHelper::build({1, 2, 3, 4, 5});
39+
ListNode* expected = listHelper::build({1, 2, 3, 5});
40+
ListNode* res = s.removeNthFromEnd(head, 2);
41+
assert(listHelper::equal(res, expected));
42+
listHelper::free(res);
43+
listHelper::free(expected);
44+
45+
head = listHelper::build({1});
46+
expected = nullptr;
47+
res = s.removeNthFromEnd(head, 1);
48+
assert(listHelper::equal(res, expected));
49+
listHelper::free(res);
50+
listHelper::free(expected);
51+
52+
head = listHelper::build({1, 2});
53+
expected = listHelper::build({1});
54+
res = s.removeNthFromEnd(head, 1);
55+
assert(listHelper::equal(res, expected));
56+
listHelper::free(res);
57+
listHelper::free(expected);
58+
}
59+
60+
int main() {
61+
test();
62+
return 0;
63+
}

‎src/2-addTwoNumbers.cpp‎

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "../include/common.h"
77

88
using namespace std;
9-
using namespace listHelper;
9+
using listHelper::ListNode;
1010

1111
class Solution {
1212
public:
@@ -30,32 +30,36 @@ class Solution {
3030

3131
void test() {
3232
Solution s;
33-
ListNode* l1 = new ListNode(vector<int>{2, 4, 3});
34-
ListNode* l2 = new ListNode(vector<int>{5, 6, 4});
35-
ListNode* expected = new ListNode(vector<int>{7, 0, 8});
33+
ListNode* l1 = listHelper::build({2, 4, 3});
34+
ListNode* l2 = listHelper::build({5, 6, 4});
35+
ListNode* expected = listHelper::build({7, 0, 8});
3636
ListNode* res = s.addTwoNumbers(l1, l2);
37-
assert(res->equal(expected));
38-
freeList(l1);
39-
freeList(l2);
40-
freeList(expected);
4137

42-
l1 = new ListNode(vector<int>{0});
43-
l2 = new ListNode(vector<int>{0});
44-
expected = new ListNode(vector<int>{0});
38+
assert(listHelper::equal(res, expected));
39+
listHelper::free(l1);
40+
listHelper::free(l2);
41+
listHelper::free(res);
42+
listHelper::free(expected);
43+
44+
l1 = listHelper::build({0});
45+
l2 = listHelper::build({0});
46+
expected = listHelper::build({0});
4547
res = s.addTwoNumbers(l1, l2);
46-
assert(res->equal(expected));
47-
freeList(l1);
48-
freeList(l2);
49-
freeList(expected);
48+
assert(listHelper::equal(res, expected));
49+
listHelper::free(l1);
50+
listHelper::free(l2);
51+
listHelper::free(res);
52+
listHelper::free(expected);
5053

51-
l1 = new ListNode(vector<int>{9, 9, 9, 9, 9, 9, 9});
52-
l2 = new ListNode(vector<int>{9, 9, 9, 9});
53-
expected = new ListNode(vector<int>{8, 9, 9, 9, 0, 0, 0, 1});
54+
l1 = listHelper::build({9, 9, 9, 9, 9, 9, 9});
55+
l2 = listHelper::build({9, 9, 9, 9});
56+
expected = listHelper::build({8, 9, 9, 9, 0, 0, 0, 1});
5457
res = s.addTwoNumbers(l1, l2);
55-
assert(res->equal(expected));
56-
freeList(l1);
57-
freeList(l2);
58-
freeList(expected);
58+
assert(listHelper::equal(res, expected));
59+
listHelper::free(l1);
60+
listHelper::free(l2);
61+
listHelper::free(res);
62+
listHelper::free(expected);
5963
}
6064

6165
int main() {

0 commit comments

Comments
 (0)