019Remove Nth Node From End of List
来源:互联网 发布:网络p2p靠谱吗 编辑:程序博客网 时间:2024/06/07 00:35
删除倒数第N个链表节点,快慢指针,快的先走几步,然后慢点再走,如果是第一个需要特殊处理一下,其他情况直接返回即可,还需要提高解题速度啊....
class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode pAhead = head;ListNode pBehind = head; //快指针先走n步 ,题目说了n为有效值,所以不用判断n的有效性for (int i = 0; i < n; i++) {pAhead = pAhead.next;} //如果是倒数第N个,即第一个head需要处理一下if (pAhead == null) {return head.next;}while (pAhead != null && pAhead.next != null) {pAhead = pAhead.next;pBehind = pBehind.next;}pBehind.next = pBehind.next.next;return head; }}
阅读全文
0 0
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Go sha256使用实例介绍
- vi命令总结
- Kotlin新语言使用RxJava+Retrofit请求网络数据+lambda表达式+RecyclerView展示+Glide展示图片
- java需要注意的陷阱和良好的习惯
- 成为“高维空间”的人
- 019Remove Nth Node From End of List
- Spring+junit+Activiti工作流源码免费下载
- 蓝桥杯 算法训练 矩阵乘法
- nginx 卸载后重新安装/etc/nginx配置文件没了,cannot open /etc/nginx/nginx.conf (No such file or directory)
- vue饿了么webapp之vue.set用法
- 机器学习数学基础--(一)
- 编程语言发展史以及使用场景
- Spring核心接口之InitializingBean
- 使用java代码操作mongodb