ListNode *removeNthFromEnd(ListNode *head, int n)
{
if(head==NULL || (head->next==NULL && n==1))
return NULL;
ListNode *pNode = head;
int length=0;
while(pNode!=NULL)
{
++length;
pNode=pNode->next;
}
if(n>length)
return head;
if(n==length && head->next!=NULL)
return head->next;
int rest=length-n-1;
pNode=head;
while(rest-- && pNode!=NULL)
{
pNode=pNode->next;
}
if(n==1)
pNode->next=NULL;
else
pNode->next=pNode->next->next;
return head;
}
LeetCode-Remove Nth Node From End of List
最新推荐文章于 2024-05-13 23:55:39 发布