- 删除链表的倒数第 N 个结点
/**
* 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* fast = head;
ListNode* slow = head;
for(int i = 0;i<N;i++){
fast = fast->next;
}
while(true){
if(fast == nullptr){
return head->next;
}else if(fast->next == nullptr){
slow->next = slow->next->next;
return head;
}
slow = slow->next;
fast = fast->next;
}
}
};