leetcode 19. Remove Nth Node From End of List

来源:互联网 发布:软件测试工程师职责 编辑:程序博客网 时间:2024/06/05 14:25

其实三个指针可以简化称为两个,要删除的元素不需要指针,只需要记录其后面元素的指针,最后到达应该删除的位置之后:
back -> next = back->next->next;

ListNode* removeNthFromEnd(ListNode* head, int n) {    ListNode * front = head, * rem = head, * back = head;    int m = n-1;    while(m--){        front = front -> next;    }    if(front->next==NULL){        return head->next;    }    front = front->next;    rem = rem->next;    while(front->next!=NULL){        front = front->next;        rem = rem->next;        back = back->next;    }    back ->next = rem->next;    return head;}
0 0