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;
}
};
代码: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;
}
};
相关文章推荐
- Leetcode 19. Remove Nth Node From End of List
- 【leetcode】【19】Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List (Python)
- leetcode 19:Remove Nth Node From End of List
- 19.[LeetCode]Remove Nth Node From End of List
- LeetCode----- 19.Remove Nth Node From End of List
- leetcode 19 Remove Nth Node from end of list
- 【Leetcode】19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List Leetcode Python
- leetcode-19 Remove Nth Node From End of List
- [Leetcode]#19 Remove Nth Node From End of List
- [Leetcode]19. Remove Nth Node From End of List
- Leetcode:19. Remove Nth Node From End of List(JAVA)
- [leetcode] 【链表】19. Remove Nth Node From End of List
- leetcode(8)19. Remove Nth Node From End of List
- LeetCode 19:Remove Nth Node From End of List
- <LeetCode OJ> 19. Remove Nth Node From End of List
- LeetCode 19 Remove Nth Node From End of List
- leetcode 19. Remove Nth Node From End of List
- Leetcode 19.Remove Nth Node From End of List