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

leetcode 19. Remove Nth Node From End of List

2016-06-20 12:53 531 查看
其实三个指针可以简化称为两个,要删除的元素不需要指针,只需要记录其后面元素的指针,最后到达应该删除的位置之后:

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