/**
* 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 {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
// 初始化一个空节点,初始赋值为0,并且list的下一个next指针指向head,指针指向为list
ListNode* list = new ListNode(0, head);
ListNode* first = head;
ListNode* second = list;
for(int i = 0; i < n; i++) {
first = first->next;
}
while(first != nullptr) {
second = second->next;
first = first->next;
}
ListNode* p = second->next;
second->next = second->next->next;
delete p;
ListNode* ans = list->next;
delete list;
return ans;
}
};
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!