您的位置:首页 > Web前端 > Node.js

LeetCode-Remove Nth Node From End of List

2014-09-12 00:07 316 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  List leetcode