题目:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
代码:
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *pre=head,*q=pre;
for (int i = 0; i < n; i++){
q = q->next;
}
if(q==NULL){
head = head ->next;
return head;
}
while(q->next != NULL){
q = q->next;
pre = pre->next;
}
pre ->next = pre ->next->next;
return head;
}
};