题解 | #删除链表的倒数第n个节点#
删除链表的倒数第n个节点
https://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
/** * 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 ListNode *start=new ListNode(-1); start->next=head; ListNode *pre=start; ListNode *cur=head; ListNode *fast=head; while(n--){ fast=fast->next; } while(fast!=NULL){ fast=fast->next; cur=cur->next; pre=pre->next; } pre->next=cur->next; free(cur); return start->next; } };