leetcode19 Remove Nth Node From End of List(删除链表的倒数第N个节点)
2018-10-29 17:39
771 查看
版权声明:个人博客网址 https://29dch.github.io/ GitHub网址 https://github.com/29DCH,欢迎大家前来交流探讨和fork! https://blog.csdn.net/CowBoySoBusy/article/details/83510119
题目链接
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
知识点:
考察链表、指针相关知识
思路:
1.先遍历一遍计算得到链表的长度,再遍历一遍删除倒数第n个节点
2.双指针法:p指针开始的时候指向虚拟头结点,p、q指针之间的距离一直保持为n,p
、q指针同时往链表尾移动直到q指针指向NULL,代表此时p指针指向的元素就是待删除的元素的前一个元素,再把后面那个元素删除即可.这种方法只遍历一次链表就解决问题了.
下面代码采用思路2的思路.
AC代码:
class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { assert(n>=0); ListNode* dummyhead = new ListNode(0); dummyhead->next = head; ListNode* p=dummyhead; ListNode* q=dummyhead; for(int i=0; i<n+1; i++) { assert(q); q=q->next; } while(q!=NULL) { p=p->next; q=q->next; } ListNode* delNode = p->next; p->next=delNode->next; delete delNode; ListNode* retNode = dummyhead->next; delete dummyhead; return retNode; } };
AC代码加测试如下:
#include <bits/stdc++.h> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* createLinkedList(int arr[], int n) { if(n == 0) return NULL; ListNode* head = new ListNode(arr[0]); ListNode* curNode = head; for(int i=1; i<n; i++) { curNode->next = new ListNode(arr[i]); curNode = curNode->next; } return head; } void printLinkedList(ListNode* head) { ListNode* curNode = head; while(curNode!=NULL) { cout<<curNode->val<<" -> "; curNode = curNode->next; } cout<<"NULL"<<endl; } void deleteLinkedList(ListNode* head) { ListNode* curNode = head; while(curNode!=NULL) { ListNode* delNode = curNode; curNode = curNode->next; delete delNode; } } class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { assert(n>=0); ListNode* dummyhead = new ListNode(0); dummyhead->next = head; ListNode* p=dummyhead; ListNode* q=dummyhead; for(int i=0; i<n+1; i++) { assert(q); q=q->next; } while(q!=NULL) { p=p->next; q=q->next; } ListNode* delNode = p->next; p->next=delNode->next; delete delNode; ListNode* retNode = dummyhead->next; delete dummyhead; return retNode; } }; int main() { int arr[]= {1,2,3,4,5}; int n = sizeof(arr)/sizeof(int); ListNode* head = createLinkedList(arr,n); printLinkedList(head); ListNode* head1 = Solution().removeNthFromEnd(head,2); printLinkedList(head1); deleteLinkedList(head1); return 0; }
阅读更多
相关文章推荐
- [LeetCode] 19. Remove Nth Node From End of List 删除链表的倒数第N个节点 @python
- [LeetCode-19] Remove Nth Node From End of List(删除链表中倒数第N个节点)
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- [C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)
- [LeetCode]—Remove Nth Node From End of List 删除链表的倒数第n个节点
- 【LeetCode】Remove Nth Node From End of List 删除链表中倒数第n个节点- Easy +
- 【Leetcode】19. Remove Nth Node From End of List - 删掉链表倒数第n个节点
- leetcode_[python/C++]_19. Remove Nth Node From End of List(删除链表末第n个节点)
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
- leetcode:Remove Nth Node From End of List(删除链表倒数第n个节点)【面试算法题】
- LeetCodet题解--19. Remove Nth Node From End of List(删除链表的倒数第n个元素)
- leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
- LeetCode 19. Remove Nth Node From End of List--删除链表的倒数第n个结点
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
- 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)
- LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点
- Remove Nth Node From End of List 链表删除倒数第N个元素@LeetCode
- 面试题15:链表中倒数第k个节点(Leetcode-19:Remove Nth Node From End of List)