LeetCode: Remove Nth Node From End of List
2013-09-29 11:06
447 查看
不考虑节点不存在的情况
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *cur = head,*pri=head;
while(n--){
cur = cur->next;
}
if(cur==NULL)
return head->next;
while(cur->next!=NULL){
cur = cur->next;
pri = pri->next;
}
pri->next = pri->next->next;
return head;
}
};
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *cur = head,*pri=head;
while(n--){
cur = cur->next;
}
if(cur==NULL)
return head->next;
while(cur->next!=NULL){
cur = cur->next;
pri = pri->next;
}
pri->next = pri->next->next;
return head;
}
};
相关文章推荐
- [Leetcode] Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- leetcode-Remove Nth Node From End of List-19
- Leetcode-19. Remove Nth Node From End of List
- Leetcode_remove-nth-node-from-end-of-list(c++ and python version)
- Leetcode -- Remove Nth Node From End of List
- Leetcode Remove Nth Node From End of List
- 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
- leetcode Remove Nth Node From End of List(Java)
- LeetCode Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- LeetCode "Remove Nth Node From End of List"
- LeetCode Remove Nth Node From End of List
- 【Leetcode-easy】Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- leetcode Remove Nth Node From End of List 链表 双指针
- 【LeetCode】19. Remove Nth Node From End of List