-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeleteNodeLinkedList.cpp
More file actions
144 lines (118 loc) · 2.97 KB
/
deleteNodeLinkedList.cpp
File metadata and controls
144 lines (118 loc) · 2.97 KB
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
// https://leetcode.com/problems/delete-node-in-a-linked-list/
//
// Status: Accepted
// Runtime: 8 ms
// Score: 99.75 %
//
#include <gtest/gtest.h>
#include "args.h"
#include "leetcode.h"
#include "macros.h"
using namespace std;
// class definition comes from leetcode
class Solution
{
public:
void deleteNode(ListNode *node)
{
// 4 -> 5 -> 1 -> 9 -> null;
// Shift all nodes to the left
// delete the last one
ListNode *prev = node;
while (node->next != NULL)
{
node->val = node->next->val;
prev = node;
node = node->next;
}
prev->next = NULL;
// Method is called delete node, but
// I do not know who owns it so I won't touch it
// delete node;
}
};
namespace test
{
static void create(vector<int> values, vector<ListNode> &nodes)
{
for (auto &v : values)
{
nodes.push_back(ListNode(v));
}
int N = nodes.size();
for (int n = 1; n < N; n++)
{
nodes[n - 1].next = &nodes[n];
}
}
static vector<int> unpack(vector<ListNode> &nodes)
{
vector<int> output;
if (nodes.size() == 0)
{
return output;
}
ListNode *node = &nodes[0];
while (node)
{
output.push_back(node->val);
node = node->next;
}
return output;
}
} // namespace test
TEST(DeleteNodeLinkedList, Example1)
{
Solution solution;
vector<int> input = {4, 5, 1, 9};
vector<int> output = {4, 1, 9};
vector<ListNode> nodes;
test::create(input, nodes);
solution.deleteNode(&nodes[1]);
vector<int> answer = test::unpack(nodes);
ASSERT_EQ(answer, output);
}
TEST(DeleteNodeLinkedList, Example2)
{
Solution solution;
vector<int> input = {4, 5, 1, 9};
vector<int> output = {4, 5, 9};
vector<ListNode> nodes;
test::create(input, nodes);
solution.deleteNode(&nodes[2]);
vector<int> answer = test::unpack(nodes);
ASSERT_EQ(answer, output);
}
TEST(DeleteNodeLinkedList, Example3)
{
Solution solution;
vector<int> input = {1, 2, 3, 4};
vector<int> output = {1, 2, 4};
vector<ListNode> nodes;
test::create(input, nodes);
solution.deleteNode(&nodes[2]);
vector<int> answer = test::unpack(nodes);
ASSERT_EQ(answer, output);
}
TEST(DeleteNodeLinkedList, Example4)
{
Solution solution;
vector<int> input = {0, 1};
vector<int> output = {1};
vector<ListNode> nodes;
test::create(input, nodes);
solution.deleteNode(&nodes[0]);
vector<int> answer = test::unpack(nodes);
ASSERT_EQ(answer, output);
}
TEST(DeleteNodeLinkedList, Example5)
{
Solution solution;
vector<int> input = {-3, 5, -99};
vector<int> output = {5, -99};
vector<ListNode> nodes;
test::create(input, nodes);
solution.deleteNode(&nodes[0]);
vector<int> answer = test::unpack(nodes);
ASSERT_EQ(answer, output);
}