文章来源地址https://uudwc.com/A/k9jOk
/**
* 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) {
ListNode* x=new ListNode(0);
x->next=head;
ListNode *f=x;
ListNode *s=x;
n=n+1;
while(n--){
f=f->next;
}
while(f){
s=s->next;
f=f->next;
}
s->next=s->next->next;
return x->next;
}
};
文章来源:https://uudwc.com/A/k9jOk