/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
if(!head) return NULL;
int i=0;
struct ListNode* tem=head;
struct ListNode* pre=NULL;
int j=0;
while(tem){
j++;
tem=tem->next;
}
tem=head;
if(n>j) return head;
if(n==j){
head=head->next;
free(tem);
return head;
}
while(i!=j-n && tem){
i++;
pre=tem;
tem=tem->next;
}
if(!tem) return head;
pre->next=tem->next;
free(tem);
return head;
}
结果:
标签:head,ListNode,struct,19,next,链表,return,倒数第,tem From: https://www.cnblogs.com/llllmz/p/18040927