19. Remove Nth Node From End of List(移除链表的倒数第n个节点)
2017-10-01 09:59
639 查看
问题描述
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Note:
Given n will always be valid.
Try to do this in one pass.
题目分析
这道题目很明朗,要我们移除链表倒数第n个节点,并且已经限定n一定是有效的,即n不会大于链表中的元素总数。传统思路很简单,可以通过两次遍历,第一次求出链表的长度,第二次删除要删除的节点。但是题目要求我们一次遍历解决问题,那么就得想些比较巧妙的方法了。我们必须通过一次遍历找到倒数第N个节点,那么我们需要用两个指针来帮助我们解题p和q。首先q指针先向前走n步,与p指针保持n个节点的距离,然后p指针和q指针同时向链表尾移动,直到q为最后一个元素时停止,此时p指向要移除元素的前一个元素,我们只需要删除p的下一个节点即可。
代码展示
运行结果展示
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
题目分析
这道题目很明朗,要我们移除链表倒数第n个节点,并且已经限定n一定是有效的,即n不会大于链表中的元素总数。传统思路很简单,可以通过两次遍历,第一次求出链表的长度,第二次删除要删除的节点。但是题目要求我们一次遍历解决问题,那么就得想些比较巧妙的方法了。我们必须通过一次遍历找到倒数第N个节点,那么我们需要用两个指针来帮助我们解题p和q。首先q指针先向前走n步,与p指针保持n个节点的距离,然后p指针和q指针同时向链表尾移动,直到q为最后一个元素时停止,此时p指向要移除元素的前一个元素,我们只需要删除p的下一个节点即可。
代码展示
#include <iostream> #include <stdlib.h> #include <string> using namespace std; struct ListNode { //定义链表结构 int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if (head->next==NULL) return NULL; //判断链表是否只有1个节点 ListNode *p = head, *q = head; //定义两个指针 while(n--) q = q->next; //使p指针与q指针相距n个节点 if (q == NULL) return head->next; while(q->next) { //p指针与q指针同时向链表末尾遍历,直至q指针指向链表尾 q = q->next; p = p->next; } p->next = p->next->next; //此时p指针指向要删除节点的上一个节点,删除p节点的下一个节点即可 return head; } }; int main(){ cout<<"输入链表的长度:"; int n; cin>>n; ListNode* head=NULL; ListNode* p; int a; for(int i=0;i<n;i++){ cin>>a; if (head == NULL){ head = (ListNode *)malloc(sizeof(ListNode)); head->val = a; p = head; } else{ p->next = (ListNode *)malloc(sizeof(ListNode)); p = p->next; p->val = a; } } p->next=NULL; Solution solution; cout<<"输入你想要删除的倒数节点序号:"; int b; cin>>b; ListNode* result = solution.removeNthFromEnd(head,b); while(result!=NULL){ cout<<result->val<<" "; result=result->next; } cout<<endl; return 0; }
运行结果展示
相关文章推荐
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
- 19. Remove Nth Node From End of List (移除链表倒数第n个节点)
- 【Leetcode】19. 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个节点
- [LeetCode] 19. Remove Nth Node From End of List 删除链表的倒数第N个节点 @python
- 【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 3. 移除链表的倒数第n个节点 Remove Nth Node From End of List
- [LeetCode-19] Remove Nth Node From End of List(删除链表中倒数第N个节点)
- 174. 删除链表中倒数第n个节点 (remove-nth-node-from-end-of-list)(c++)----lintcode面试题之链表
- 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个结点)
- LeetCode(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个结点