forked from LeetCode-in-Cpp/LeetCode-in-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
39 lines (36 loc) · 1.03 KB
/
Solution.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
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Two_Pointers #Linked_List
// #Algorithm_I_Day_5_Two_Pointers #Level_2_Day_3_Linked_List #Big_O_Time_O(L)_Space_O(L)
// #2024_05_23_Time_0_ms_(100.00%)_Space_13.2_MB_(92.45%)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
private:
int n;
void removeNth(ListNode* node) {
if (node->next == nullptr) {
return;
}
removeNth(node->next);
n--;
if (n == 0) {
node->next = node->next->next;
}
}
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
this->n = n;
ListNode* node = new ListNode(0, head);
removeNth(node);
ListNode* newHead = node->next;
delete node; // clean up the temporary node
return newHead;
}
};