LeetCode-Remove Nth Node From End of List

来源:互联网 发布:淘宝网店铺怎么装修 编辑:程序博客网 时间:2024/06/06 03:51
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;}

0 0
原创粉丝点击