LeetCode #19 删除链表的倒数第N个节点
作者:
HU兔兔 | 来源:发表于
2020-02-09 16:29 被阅读0次/**
* 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* p=head;
int i=0;
while(i<n&&p->next!=NULL){
p=p->next;
i++;
}
if(i<n){
return head->next;
}
ListNode* q=head;
while(p->next!=NULL){
p=p->next;
q=q->next;
}
q->next=q->next->next;
return head;
}
};
本文标题:LeetCode #19 删除链表的倒数第N个节点
本文链接:https://www.haomeiwen.com/subject/tfhnxhtx.html
网友评论