/**
* 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* newHead= new ListNode(-1);
newHead->next=head;
ListNode* first=newHead;
ListNode* second=head;
while(--n)
second=second->next;
while(second->next!=NULL)
{
first=first->next;
second=second->next;
}
first->next=first->next->next;
return newHead->next;
}
};
Remove Nth Node From End of List
最新推荐文章于 2020-08-05 23:20:29 发布