/**
* 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* res = head;
int count = 0;
while(res)
{
res = res->next;
++count;
}
if(count<n||head==NULL)
return head;
res = head;
int i = 0;
if(count==n)
{
res = head->next;
delete head;
return res;
}
while(i<count-n-1)
{
res = res->next;
++i;
}
res->next = res->next->next;
return head;
}
};
19 Remove Nth Node From End of List
最新推荐文章于 2024-06-20 12:29:29 发布
