-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-nth-node-from-end-of-list.cpp
44 lines (37 loc) · 1.2 KB
/
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
// For this solution we use 2 pointers to create a gap size of n
// Once that gap is created, we can simply move both pointers forward one until we hit
// the end on the 2nd one. This will allow us to reach the end.
ListNode* removeNthFromEnd(ListNode* head, int n) {
// We start out with a new pointer to check where to begin
ListNode * begin = new ListNode(0);
ListNode * fast = begin;
ListNode * slow = begin;
begin->next = head;
int count = 0;
// We move the fast pointer to be ahead of the slower pointer by N
while(count < n+ 1)
{
fast = fast->next;
count++;
}
// We move the pointers at the end to skip the nth pointer
while(fast != nullptr)
{
fast = fast->next;
slow = slow->next;
}
// We skip the pointer to be deleted
slow->next = slow->next->next;
return begin->next;
}
};