代码实现:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeNthFromEnd(struct ListNode *head, int n) { if (head == NULL || n == 0) { return head; } int i = n; struct ListNode *h = malloc(sizeof(*h)); h->next = head; struct ListNode *p = h; struct ListNode *pp = head; while (i--) { pp = pp->next; } while (pp) { pp = pp->next; p = p->next; } struct ListNode *pc = p->next; p->next = pc->next; pc->next = NULL; free(pc); return h->next; }