Remove Nth Node From End of List
2017-12-06 15:51
323 查看
Remove Nth Node From End of List
the most important thing is that you must know the length of the list;
l is length;
then remove is easy
And the complete code
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int l = 0;
ListNode* beg = new ListNode(0); // you should create a new ListNode
beg->next = head;
ListNode* first = head;
while(first)
{
l++;
first = first->next;
}
l = l - n;
first = beg;
while(l>0)
{
l--;
first = first->next;
}
first->next = (first->next)->next;
return beg->next;
}
};
the most important thing is that you must know the length of the list;
l is length;
then remove is easy
And the complete code
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int l = 0;
ListNode* beg = new ListNode(0); // you should create a new ListNode
beg->next = head;
ListNode* first = head;
while(first)
{
l++;
first = first->next;
}
l = l - n;
first = beg;
while(l>0)
{
l--;
first = first->next;
}
first->next = (first->next)->next;
return beg->next;
}
};
相关文章推荐
- LeetCode---Remove Nth Node From End of List
- LeetCode 19 Remove Nth Node From End of List (C,C++,Java,Python)
- LeetCode 19. Remove Nth Node From End of List
- 个人记录-LeetCode 19. Remove Nth Node From End of List
- 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
- LeetCode刷题笔录Remove Nth Node from End of List
- leetcode: Remove Nth Node From End of List
- 19. Remove Nth Node From End of List
- 19.[LeetCode]Remove Nth Node From End of List
- LeetCode:M-19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List
- 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
- Remove Nth Node From End of List "移除链表中的倒数第N项"
- 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