/**
* 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) {
if(head == NULL || head->next == NULL)
return NULL;
ListNode * first = head;
ListNode * second = head;
for(int i = 0;i < n;i++)
first = first->next;
if(first == NULL)
return head->next;
while(first->next != NULL)
{
first = first->next;
second = second->next;
}
second->next = second->next->next;
return head;
}
};
19. Remove Nth Node From End of List
最新推荐文章于 2021-06-12 22:13:51 发布