1.直接看代码吧
思路在代码注释前边很简单的题
cpp
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
if(head == NULL){
return NULL;
}
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
dummy->next = head;
struct ListNode* pre = dummy;
while(n>0){
head = head->next;
n--;
}
while(head!=NULL){
pre = pre->next;
head = head->next;
}
pre->next = pre->next->next;
return dummy->next;
}