不考虑节点不存在的情况
class Solution {
public:ListNode *removeNthFromEnd(ListNode *head, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *cur = head,*pri=head;
while(n--){
cur = cur->next;
}
if(cur==NULL)
return head->next;
while(cur->next!=NULL){
cur = cur->next;
pri = pri->next;
}
pri->next = pri->next->next;
return head;
}
};
本文介绍了一种高效的方法来删除链表中倒数第N个节点。通过使用双指针技术,该算法仅需遍历一次链表即可完成操作,大大提高了效率。
595

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



