https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
思路快慢指针 , 审题!!!
代码1
//注意审题 是倒数n个结点!!!
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head ||!head->next) {
return NULL;
}
ListNode *fast = head, *slow = head;
for(int i = 0; i < n; i ++) {
fast = fast -> next;// fast比slow先走n步
}
if(fast == NULL) { // 删除头结点
return head->next;
}
while(fast -> next) {
fast = fast->next;
slow = slow->next;
}
slow ->next= slow->next->next;
return head;
}
};



