feat: 19_remove_nth_node_from_end_of_list

This commit is contained in:
SquidSpirit 2025-02-21 00:05:13 +08:00
parent 8a167ebab1
commit ef2e0d8510

View File

@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;
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 {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
vector<ListNode*> pointerTable;
int sz = 0;
ListNode* currentNode = head;
while (currentNode != nullptr) {
sz++;
pointerTable.push_back(currentNode);
currentNode = currentNode->next;
}
if (sz == 1) return nullptr;
if (sz - n - 1 < 0) return head->next;
currentNode = pointerTable[pointerTable.size() - n - 1];
ListNode* target = currentNode->next;
currentNode->next = currentNode->next->next;
delete target;
return head;
}
};