-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLC19.cpp
More file actions
39 lines (28 loc) · 796 Bytes
/
LC19.cpp
File metadata and controls
39 lines (28 loc) · 796 Bytes
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
/*
19. Remove Nth Node From End of List
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
*/
class Solution {
public:
Solution() {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* first = dummy;
ListNode* second = dummy;
for (int = 0; i < n + 1; i++) {
first = first->next;
}
while (first != nullptr) {
first = first->next;
second = second->next;
}
second->next = second->next->next;
return dummy->next;
}
};