/**
* 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
最新推荐文章于 2020-08-05 23:20:29 发布