class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* Head=new ListNode(-1);
Head->next=head;
ListNode* pre=Head,*cur=Head;
int len=0;
while(cur&&len<=n)
{
len++;
cur=cur->next;
}
while(cur)
{
cur=cur->next;
pre=pre->next;
}
pre->next=pre->next->next;
head=Head->next;
delete Head;
return head;
}
};
19. Remove Nth Node From End of List
最新推荐文章于 2022-06-04 16:46:52 发布