题目:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* previous=new ListNode(1);
ListNode* current=head;
int length=1;
if(head!=NULL){
while(current->next!=NULL){
length++;
current=current->next;
}
}
int num=length-n+1;
current=head;
if(num==1){
head=head->next;
}
while(num>1){
if(current->next!=NULL){
previous=current;
current=current->next;
}
num--;
}
previous->next=current->next;
return head;
}
};