-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path19.cpp
42 lines (31 loc) · 898 Bytes
/
19.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
// 19. Remove Nth Node From End of List - https://leetcode.com/problems/remove-nth-node-from-end-of-list
#include "bits/stdc++.h"
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* answer = new ListNode(0);
ListNode* p1 = answer;
ListNode* p2 = answer;
answer->next = head;
for (int index = 1; index <= n; ++index) {
if (p2 == nullptr) { return nullptr; }
p2 = p2->next;
}
while(p2->next != nullptr) {
p1 = p1->next;
p2 = p2->next;
}
p1->next = p1->next->next;
return answer->next;
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}