/
removeNthFromEnd.cpp
104 lines (85 loc) · 2.03 KB
/
removeNthFromEnd.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
// https://leetcode.com/problems/remove-nth-node-from-end-of-list
//
// Status: Accepted
// Runtime: 0 ms
// Score: 100%
//
#include <array>
#include <iostream>
#include <vector>
#include <gtest/gtest.h>
#include "args.h"
#include "leetcode.h"
#include "macros.h"
using namespace std;
class Solution
{
public:
ListNode *to_list(vector<int> input, vector<ListNode> &nodes)
{
ListNode *head = NULL;
if (input.size())
{
head = &nodes[0];
int i = 0;
ListNode *prev = NULL;
for (auto &v : input)
{
nodes[i].val = v;
nodes[i].next = NULL;
if (prev)
{
prev->next = &nodes[i];
}
prev = &nodes[i];
i++;
}
}
return head;
}
vector<int> from_list(ListNode *head)
{
vector<int> output;
ListNode *node = head;
while (node)
{
output.push_back(node->val);
node = node->next;
}
return output;
}
ListNode *removeNthFromEnd(ListNode *head, int n)
{
std::array<ListNode *, 30> nodes;
ListNode *node = head;
int sz = 0;
while (node)
{
nodes[sz++] = node;
node = node->next;
}
int del = sz - n;
if (del == 0)
{
head = head->next;
}
else
{
ListNode *prev = nodes[del - 1];
prev->next = nodes[del]->next;
}
return head;
}
};
TEST(RemoveNthFromEnd, Example)
{
Solution solution;
vector<int> input{1, 2, 3, 4, 5};
vector<int> output{1, 2, 3, 5};
int n = 2;
vector<ListNode> inputNodes(input.size()), outputNodes;
ListNode *input_list = solution.to_list(input, inputNodes);
ListNode *output_nodes = solution.removeNthFromEnd(input_list, n);
vector<int> answer = solution.from_list(output_nodes);
ASSERT_EQ(answer, output);
}