/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
ListNode* removeNthFromEnd(ListNode* head, int n) {
// write code here
int count = 0;
ListNode *p = head;
ListNode *q;
while(p!= NULL){
count++;
p = p->next;
}
if(count == n){
p = head->next;
head->next = NULL;
return p;
}
count = count-n;
p = head;
while(count > 0){
q = p;
p = p->next;
if(count == 1){
if(p != NULL){
q->next = p->next;
p = NULL;
}else{
q->next = NULL;
}
}
count--;
}
return head;
}
};