/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *q = head;
int c = n;
while(c-- && q!=NULL){
q = q->next;
}
if(q == NULL) return head->next;
ListNode *p = head;
while(q->next){
p = p->next;
q = q->next;
}
ListNode *temp = p->next;
p->next = temp->next;
delete temp;
return head;
}
};
Remove Nth Node From End of List
最新推荐文章于 2024-06-20 12:29:29 发布
本文介绍了一种用于删除单链表倒数第N个节点的算法,通过双指针技巧实现,提高了操作效率。算法详细步骤及复杂度分析尽在其中。
652

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



