-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0019_Remove_Nth_Node_From_End_of_List.cpp
81 lines (67 loc) · 1.86 KB
/
0019_Remove_Nth_Node_From_End_of_List.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
#include<iostream>
#include<vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
void print_list(ListNode * head){
ListNode * p = head;
while(p != NULL){
cout << p->val << " ";
p = p->next;
}
cout << endl;
}
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode * p = head;
vector<ListNode*> circle;
int csize = 0;
int chead = 0;
// circle queue for pointer, max_size = n + 1
if(n <= 0) return head;
while(p != NULL){
if(csize < n + 1){
circle.push_back(p);
csize++;
}
else if(csize == n + 1){
circle[chead] = p;
chead = (chead + 1) % (n + 1);
}
p = p->next;
}
// submit if free error: AddressSanitizer: alloc-dealloc-mismatch
if(csize == n){
head = circle[0]->next;
free(circle[0]);
}
if(csize == n + 1){
int ind = (chead + 1) % (n + 1);
circle[chead]->next = circle[ind]->next;
free(circle[ind]);
}
return head;
}
};
int main (){
Solution solve;
vector<int> inputs = {1, 2, 3, 4, 5};
vector<vector<int> > outputs;
ListNode * head = new ListNode(inputs[0]);
ListNode * p = head;
for(int i = 1; i < inputs.size(); i++){
ListNode * cur = new ListNode(inputs[i]);
p->next = cur;
p = cur;
}
cout << "Inputs: " << endl;
print_list(head);
p = solve.removeNthFromEnd(head, 2);
cout << "Outputs: " << endl;
print_list(p);
return 0;
}