/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (head == NULL)
return NULL;
ListNode *pCur;
ListNode *pPreN;
if (n == 1 && head->next == NULL)
return NULL;
int i = n;
pCur = head;
while (i > 0 && pCur->next != NULL)
{
pCur = pCur->next;
i--;
}
if (i != 0)
{
if (i == 1)
return head->next;
else
return NULL;
}
pPreN = head;
while (pCur->next != NULL)
{
pCur = pCur->next;
pPreN = pPreN->next;
}
pPreN->next = pPreN->next->next;
return head;
}
};
leetcode: Remove Nth Node From End of List
最新推荐文章于 2020-08-05 23:20:29 发布