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

leetcode --19. Remove Nth Node From End of List

2017-08-14 15:50 393 查看
题目:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/

代码:class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *pre=head,*q=pre;
for (int i = 0; i < n; i++){
q = q->next;
}
if(q==NULL){
head = head ->next;
return head;
}
while(q->next != NULL){
q = q->next;
pre = pre->next;
}
pre ->next = pre ->next->next;
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: