struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
struct ListNode *dummy = malloc(sizeof(struct ListNode));
dummy->val = 0,dummy->next = head;
struct ListNode *pre ;
struct ListNode*p;
pre = dummy,p = head;
for(int i = 0;i<n;++i){
p = p->next;
}
if(p==NULL){
return head->next;
}
while(p ){
p = p->next;
pre = pre->next;
}
pre->next = pre->next->next;
return head;
}
力扣中档题:删除链表的倒数第k个节点
最新推荐文章于 2024-09-09 00:44:52 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)