leetcode-19-Remove Nth Node From End of List
来源:互联网 发布:普罗米修斯白板的软件 编辑:程序博客网 时间:2024/05/21 09:41
题意:删除链表从后往前开始的第n个节点。
单链表的操作。别人说用python来实现单链表没有意义,好吧我不知道怎么反驳暂且附和好了。python用list可以实现堆栈和队列(pop(0)啥的…)。熟悉了链表的操作之后,本题的意思就在于从后往前的数,两个指针。第一个先走n步,然后第二个出发,到第一个继续走到最后一个元素的时候,第二个此时的位置的节点就是我们要删除的节点。
要注意的是可能要删除的是第一个元素。或者开始的时候链表即为空或者只有一个元素的时候。删除的操作在数据结构里面学过,主要的就是如何用编程语言实现,要温习数据结构。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { if (head==NULL) { return NULL; } ListNode* pre=NULL; ListNode* p=head; ListNode* q=head; for (int i=0;i<n-1;i++) { q=q->next; } while(q->next) { pre=p; p=p->next; q=q->next; } if (pre==NULL) { head=head->next; delete p; } else { pre->next=p->next; delete p; } return head; }};
0 0
- [Leetcode]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, Easy] 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 Solutions
- 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
- LeetCode | #19 Remove Nth Node From End of List
- leetcode.19----------Remove Nth Node From End of List
- [Leetcode 19, Easy] 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 19 Remove Nth Node From End of List
- leetcode 19 Remove Nth Node From End of List
- LeetCode-27.Remove Element
- c++中字符串的输入
- JSONKit 解析后存在NSULL对象导致的Crash问题处理
- reactnative滑动删除
- C# HttpWebRequest 请求 常出现的问题
- leetcode-19-Remove Nth Node From End of List
- iOS 统计代码行数
- Android安全攻防战,反编译与混淆技术完全解析(下)
- 网络仿真工具TOTEM之——mpls构建LSP
- 计算机视觉/机器学习/深度学习预备知识
- pytest skip函数
- UICollectionView
- DPM(Defomable Parts Model) 源码分析-检测(二)
- 图片缓存思路