ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head->next)return NULL;
ListNode* p=head,*q=head;
for(int i=0;i<n;i++)q=q->next;
if(!q)return head->next;
while(q->next)
{
q=q->next;
p=p->next;
}
p->next=p->next->next;
return head;
}
本文介绍了一种高效算法,用于在链表中删除倒数第N个节点,通过双指针技巧实现,避免了两次遍历链表的复杂度。

被折叠的 条评论
为什么被折叠?



